I am getting a null pointer exception when a user enters date in wrong format.
Method to convert String to Date
Date stringToDate(String dateString) {
Date returnDate = null;
if (dateString!= null && dateString.length() > 0 && isValidDate(dateString)) {
returnDate = dateFormat.parse(dateStr);
}
return returnDate;
}
and
boolean isValidDate(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Pattern datePattern = Pattern.compile("[0-9]{2}/[0-9]{2}/[0-9]{4}");
Matcher datePatternMatch = datePattern.matcher(date);
boolean datePatternMatchfound = datePatternMatch.matches();
if(date==null){
return false;
} else if(date!=null && date.length()>0){
if(datePatternMatchfound){
sdf.setLenient(false);
sdf.parse(date.trim());
}
return true;
} else {
return false;
}
}
I am just curious to know ....
1) what should be valid pattern for date?
2) if the user enters wrong date stringToDate method will certainly get failed and throw a null pointer exception. How to avoid that?
Any help would really be appreciated.
you are assuming the SimpleDateFormat(MM-dd-yyyyas the default pattern the user will input, either you should make sure your user can only enter in SimpleDateFormat, or you should make changes in isValidDate() to accept
Correct format of date representation depends entirely on your application and user locale. You can however limit the input format to a certain format and use it to parse and format the date input.
You can catch the ParseException thrown by the parse method and handle invalid cases inside your catch clause.
For example your code can be simplified to following:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date stringToDate(String dateString) {
try {
Date returnDate = sdf.parse(dateString);
// do something with returnDate, if necessary
return returnDate;
} catch(ParseException e) {
// Date is not valid, handle invalid cases here..
return null; // Not a good practice, you probably would throw an Exception
}
}
And you can use the same formatter to display your values in the user interface by calling sdf.format(someDate) method and getting the String representation.
One thing is that you need to be more defensive in your validation method.
In the code you have shown, you do this:
Matcher datePatternMatch = datePattern.matcher(date);
before you check whether the date String is null. Pattern.matcher(null) results in NullPointerException.
So you need to move this code within your if (date != null) conditional block.
Aside from that, I don't see a benefit in validating the date String with a regex before validating it with a DateFormat. The regex validation is not giving you any additional benefit.
The valid pattern format depends for instance on the country setting of system.
You should put the content of your isValidDate() method in a try-catch block to avoid an exception.
By using Simple date format class we can validate if the string is date or not. You need to make sure to set the setLenient(false) to the simple date format object.
If it's not set you are end up with issue by rounding values.For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1.
Example code:
http://www.vijayakumarg.co.in/2014/04/how-to-validate-date-string-in-java.html
public static boolean validateJavaDate(String strDate)
{
/*
* Set preferred date format,
* For example MM-dd-yyyy, MM.dd.yyyy,dd.MM.yyyy etc.*/
SimpleDateFormat sdfrmt = new SimpleDateFormat("MM/dd/yyyy");
sdfrmt.setLenient(false);
/* Create Date object */
Date javaDate = null;
/* parse the string into date form */
try
{
javaDate = sdfrmt.parse(strDate);
System.out.println("Date after validation: " + javaDate);
}
/* Date format is invalid */
catch (ParseException e)
{
System.out.println("The date you provided is in an " +"invalid date format.");
return false;
}
/* Return 'true' - since date is in valid format */
return true;
}
Related
I implemented the following method that can be used to convert a String to an accepted format:
public static Date parseDate(final String dateString, final String[] acceptedFormats) {
for (String format : acceptedFormats) {
try {
return new SimpleDateFormat(format).parse(dateString);
} catch (ParseException e) {
// dateString does not have this format
}
}
return null; // dateString does not match any accepted format
}
As I read from some Java books, its not a good practice to use exceptions to control the flow. In this case, I use an empty catch block and thats not a good practice. Can you help me to to write a method that does the same thing but in a different way?
I have to mention that I am not allowed to use external libs( I know that there a lib from apachee that can do this thing in a nice manner)
You can use the overload of parse which takes a ParsePosition:
public static Date parseDate(final String dateString, final String[] acceptedFormats) {
ParsePosition position = new ParsePosition(0);
for (String format : acceptedFormats) {
// TODO: Specify time zone and locale
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = format.parse(text, position);
if (date != null && position.getIndex() == text.length()) {
return date;
}
position.setIndex(0);
}
return null; // dateString does not match any accepted format
}
Note the check that the parse position is the length of the string - that prevents false positives where some of the text has been parsed, but there's more left over - such as a format of "yyyy-MM-dd" and text of "2015-07-02T15:40".
I have the below method in which date is coming as parameter that is in form of string and that parameter name is dateString as shown below and ultimately the date is converted and stored n form of java.sql.Date which is also return type of this method.
public static java.sql.Date getSimpleDate11(String dateString) {
if (dateString == null) {
return null;
}
java.util.Date date = null;
java.sql.Date sqlDate = null;
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
date = df.parse(dateString);
sqlDate = new java.sql.Date(date.getTime());
} catch (Exception pe) {
throw new RuntimeException(
"The date entered is invalid or has incorrect format"
+ dateString);
}
return sqlDate;
}
Question:
I found that value is coming in this format 2014-07-23 (YYYY-MM-dd) and I want the return date (java.sql..Date) to be in 23-07-14 (dd-MM-YY).
This generates the date format you want - just use String as the return type instead of java.sql.Date:
public static String getSimpleDate11(String dateString) {
if (dateString == null) {
return null;
}
DateFormat dfIn = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dfOut = new SimpleDateFormat("dd-MM-yy");
try {
Date date = dfIn.parse(dateString);
return dfOut.format(date);
} catch (ParseException e) {
throw new RuntimeException(
"The date entered is invalid or has incorrect format"
+ dateString);
}
}
In case you need to convert the incoming "yyyy-MM-dd" String date to a java.sql.Date object you already did everything right in the code you posted in your question.
A java.sql.Date object, just like a java.util.Date, stores the date you give it internally in some format you, as a programmer, don't have to care about. You just have to know that the date is stored in the object and that you can get it out of the object whenever you need it. If you're interested in technical details you can google for it but, as I said, in your case this doesn't matter.
Whenever you need a java.util.Date object just use the one you get out of your original getSimpleDate11(...) function. Whenever you need a String representation of the date in a certain format, like "dd-MM-yy", take the java.util.Date and plug it into a DateFormat object initialized with the output format you want, just like I did in my first answer with DateFormat dfOut (the format(...) method of DateFormat can handle both, java.util.Date and java.sql.Date objects).
This question already has answers here:
How to sanity check a date in Java
(24 answers)
Closed 4 years ago.
I wanted to know that is there any method available in Java that can do this.Otherwise I may go for Regex solution.
I have input string from user that can be any characters. And I want to check that the input string is according to my required date format or not.
As I have input 20130925 and my required format is dd/MM/yyyy so, for this case I should get false.
I don't want to convert this date I just want to check whether input string is according to required date format or not.
I have tried following
Date date = null;
try {
date = new SimpleDateFormat("dd/MM/yyyy").parse("20130925");
} catch (Exception ex) {
// do something for invalid dateformat
}
but my catch (Exception ex) block is unable to catch any exceptions generated by SimpleDateFormat.Parse();
Disclaimer
Parsing a string back to date/time value in an unknown format is inherently impossible (let's face it, what does 3/3/3 actually mean?!), all we can do is "best effort"
Important
This solution doesn't throw an Exception, it returns a boolean, this is by design. Any Exceptions are used purely as a guard mechanism.
2018
Since it's now 2018 and Java 8+ has the date/time API (and the rest have the ThreeTen backport). The solution remains basically the same, but becomes slightly more complicated, as we need to perform checks for:
date and time
date only
time only
This makes it look something like...
public static boolean isValidFormat(String format, String value, Locale locale) {
LocalDateTime ldt = null;
DateTimeFormatter fomatter = DateTimeFormatter.ofPattern(format, locale);
try {
ldt = LocalDateTime.parse(value, fomatter);
String result = ldt.format(fomatter);
return result.equals(value);
} catch (DateTimeParseException e) {
try {
LocalDate ld = LocalDate.parse(value, fomatter);
String result = ld.format(fomatter);
return result.equals(value);
} catch (DateTimeParseException exp) {
try {
LocalTime lt = LocalTime.parse(value, fomatter);
String result = lt.format(fomatter);
return result.equals(value);
} catch (DateTimeParseException e2) {
// Debugging purposes
//e2.printStackTrace();
}
}
}
return false;
}
This makes the following...
System.out.println("isValid - dd/MM/yyyy with 20130925 = " + isValidFormat("dd/MM/yyyy", "20130925", Locale.ENGLISH));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 = " + isValidFormat("dd/MM/yyyy", "25/09/2013", Locale.ENGLISH));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = " + isValidFormat("dd/MM/yyyy", "25/09/2013 12:13:50", Locale.ENGLISH));
System.out.println("isValid - yyyy-MM-dd with 2017-18--15 = " + isValidFormat("yyyy-MM-dd", "2017-18--15", Locale.ENGLISH));
output...
isValid - dd/MM/yyyy with 20130925 = false
isValid - dd/MM/yyyy with 25/09/2013 = true
isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = false
isValid - yyyy-MM-dd with 2017-18--15 = false
Original Answer
Simple try and parse the String to the required Date using something like SimpleDateFormat
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(value);
if (!value.equals(sdf.format(date))) {
date = null;
}
} catch (ParseException ex) {
ex.printStackTrace();
}
if (date == null) {
// Invalid date format
} else {
// Valid date format
}
You could then simply write a simple method that performed this action and returned true when ever Date was not null...
As a suggestion...
Updated with running example
I'm not sure what you are doing, but, the following example...
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateParser {
public static void main(String[] args) {
System.out.println("isValid - dd/MM/yyyy with 20130925 = " + isValidFormat("dd/MM/yyyy", "20130925"));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 = " + isValidFormat("dd/MM/yyyy", "25/09/2013"));
System.out.println("isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = " + isValidFormat("dd/MM/yyyy", "25/09/2013 12:13:50"));
}
public static boolean isValidFormat(String format, String value) {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(value);
if (!value.equals(sdf.format(date))) {
date = null;
}
} catch (ParseException ex) {
ex.printStackTrace();
}
return date != null;
}
}
Outputs (something like)...
java.text.ParseException: Unparseable date: "20130925"
isValid - dd/MM/yyyy with 20130925 = false
isValid - dd/MM/yyyy with 25/09/2013 = true
isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = false
at java.text.DateFormat.parse(DateFormat.java:366)
at javaapplication373.JavaApplication373.isValidFormat(JavaApplication373.java:28)
at javaapplication373.JavaApplication373.main(JavaApplication373.java:19)
Not correct. For isValidFormat("yyyy-MM-dd", "2017-18--15"); not throw any Exception.
isValid - yyyy-MM-dd", "2017-18--15 = false
Seems to work as expected for me - the method doesn't rely on (nor does it throw) the exception alone to perform it's operation
For your case, you may use regex:
boolean isValidFormat = input.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})");
For a larger scope or if you want a flexible solution, refer to MadProgrammer's answer.
Edit
Almost 5 years after posting this answer, I realize that this is a stupid way to validate a date format. But i'll just leave this here to tell people that using regex to validate a date is unacceptable
You can try this to simple date format valdation
public Date validateDateFormat(String dateToValdate) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HHmmss");
//To make strict date format validation
formatter.setLenient(false);
Date parsedDate = null;
try {
parsedDate = formatter.parse(dateToValdate);
System.out.println("++validated DATE TIME ++"+formatter.format(parsedDate));
} catch (ParseException e) {
//Handle exception
}
return parsedDate;
}
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
formatter.setLenient(false);
try {
Date date= formatter.parse("02/03/2010");
} catch (ParseException e) {
//If input date is in different format or invalid.
}
formatter.setLenient(false) will enforce strict matching.
If you are using Joda-Time -
private boolean isValidDate(String dateOfBirth) {
boolean valid = true;
try {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime dob = formatter.parseDateTime(dateOfBirth);
} catch (Exception e) {
valid = false;
}
return valid;
}
Here's a simple method:
public static boolean checkDatePattern(String padrao, String data) {
try {
SimpleDateFormat format = new SimpleDateFormat(padrao, LocaleUtils.DEFAULT_LOCALE);
format.parse(data);
return true;
} catch (ParseException e) {
return false;
}
}
For example, if you want the date format to be "03.11.2017"
if (String.valueOf(DateEdit.getText()).matches("([1-9]{1}|[0]{1}[1-9]{1}|[1]{1}[0-9]{1}|[2]{1}[0-9]{1}|[3]{1}[0-1]{1})" +
"([.]{1})" +
"([0]{1}[1-9]{1}|[1]{1}[0-2]{1}|[1-9]{1})" +
"([.]{1})" +
"([20]{2}[0-9]{2})"))
checkFormat=true;
else
checkFormat=false;
if (!checkFormat) {
Toast.makeText(getApplicationContext(), "incorrect date format! Ex.23.06.2016", Toast.LENGTH_SHORT).show();
}
A combination of the regex and SimpleDateFormat is the right answer i believe. SimpleDateFormat does not catch exception if the individual components are invalid meaning,
Format Defined: yyyy-mm-dd
input: 201-0-12
No exception will be thrown.This case should have been handled.
But with the regex as suggested by Sok Pomaranczowy and Baby will take care of this particular case.
Regex can be used for this with some detailed info for validation, for example this code can be used to validate any date in (DD/MM/yyyy) format with proper date and month value and year between (1950-2050)
public Boolean checkDateformat(String dateToCheck){
String rex="([0]{1}[1-9]{1}|[1-2]{1}[0-9]{1}|[3]{1}[0-1]{1})+
\/([0]{1}[1-9]{1}|[1]{1}[0-2]{2})+
\/([1]{1}[9]{1}[5-9]{1}[0-9]{1}|[2]{1}[0]{1}([0-4]{1}+
[0-9]{1}|[5]{1}[0]{1}))";
return(dateToCheck.matches(rex));
}
I want to check if a String is in certain pattern.
for example i want to check is a String matches the pattern: 2012-02-20.
I.E: xxxx-xx-xx when x is a number.
Is it possible? someone said regular expressions.
use this regex \d{4}-\d{2}-\d{2}
for checking use:
yourString.matches(regexString);
if you want to test if the date string is a valid date, better use SimpleDateFormat to check. don't use regex for that validation, how about month is 13? date is 50? leap years?
some example:
public boolean isValidDate(String dateString) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
df.parse(dateString);
return true;
} catch (ParseException e) {
return false;
}
}
You can do that with the SimpleDateFormat parse method:
final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
boolean matchesDateFormat(String date)
{
try
{
format.parse(date);
return true;
}
catch(ParseException e)
{
return false;
}
}
Of course, if you later go on to parse the date anyway then you can skip this and just try to parse it.
You can check that the String follow the exact format of 4 digits, a dash -, 2 digits, a dash - and 2 digits with #burning_LEGION's regex. However, it doesn't check whether the String represents a valid date. You can specify 9999-99-99 and it will pass the validation.
Using SimpleDateFormat is the proper method to check that the String is a valid date and it follows a given format of representation. SimpleDateFormat, apart from formatting a date, can also be used to parse Date from String: parse(String), parse(String, ParsePosition).
By default, SimpleDateFormat is lenient, which means it will allow nonsensical dates such as 2013-025-234 to pass. Use setLenient(boolean lenient) to false will solve this problem.
However, another problem is that it will also ignore any garbage data that is after a valid date (e.g. 2012-03-23garbage#$%$#%). Setting lenient doesn't solve this problem. We need to check the last position with parse(String, ParsePosition) method.
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
// Make the parsing strict - otherwise, it is worse than regex solution
dateFormatter.setLenient(false);
Date date = null;
ParsePosition pos = new ParsePosition(0);
date = dateFormatter.parse(inputString, pos);
if (date != null && pos.getIndex() == inputString.length()) {
// These 3 points are ensured:
// - The string only contains the date.
// - The date follows the format strictly.
// - And the date is a valid one.
} else {
// Valid date but string contains other garbage
// Or the string has invalid date or garbage
}
SimpleDateFormat will allow 2013-1-5 to pass, which I think is a reasonable leniency. If you don't want this, you can do a check against the regex before plugging the String into the parse method.
You can check following code:
public void test() {
String REG_EXP = "(\\d{4}-[0,1]?\\d{1}-[0,1,2,3]?\\d{1})"; //yyyy-mm-dd formate this can not check boundary condition something like this... 1981-02-30
String REG_EXP1 = "(\\d{4}-\\d{2}-\\d{2})"; // if u just want xxxx-xx-xx where x is number
String input = "date1 1981-09-06 wrong date 9999-22-22 date2 1981-9-09 date3 1981-11-1 date4";
Pattern pattern = Pattern.compile(REG_EXP);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
I have two strings, the first one contains an actual date, and the second one contains a date format.
I want to compare both the strings. Here is my code:
String s1 = "01/02/2012";
String s2 = "dd/MM/yyyy";
if (s1.equalsIgnoreCase(s2)){
System.out.println("true");}
else {
System.out.println("false");}
I have tried with all the string methods (like compare(), equalTo(), etc.). It's always executing the else part, i.e. the condition is always "false".
Check Using Format
if(isValidDate("01/02/2012")){
System.out.println("true");}else{
System.out.println("false");}
}
public boolean isValidDate(String inDate) {
if (inDate == null)
return false;
// set the format to use as a constructor argument
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
if (inDate.trim().length() != dateFormat.toPattern().length())
return false;
dateFormat.setLenient(false);
try {
// parse the inDate parameter
dateFormat.parse(inDate.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
// date validation using SimpleDateFormat
// it will take a string and make sure it's in the proper
// format as defined by you, and it will also make sure that
// it's a legal date
public boolean isValidDate(String date)
{
// set date format, this can be changed to whatever format
// you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
// you can read more about it here:
// http://java.sun.com/j2se/1.4.2/docs/api/index.html
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
// declare and initialize testDate variable, this is what will hold
// our converted string
Date testDate = null;
// we will now try to parse the string into date form
try
{
testDate = sdf.parse(date);
}
// if the format of the string provided doesn't match the format we
// declared in SimpleDateFormat() we will get an exception
catch (ParseException e)
{
errorMessage = "the date you provided is in an invalid date" +
" format.";
return false;
}
// dateformat.parse will accept any date as long as it's in the format
// you defined, it simply rolls dates over, for example, december 32
// becomes jan 1 and december 0 becomes november 30
// This statement will make sure that once the string
// has been checked for proper formatting that the date is still the
// date that was entered, if it's not, we assume that the date is invalid
if (!sdf.format(testDate).equals(date))
{
errorMessage = "The date that you provided is invalid.";
return false;
}
// if we make it to here without getting an error it is assumed that
// the date was a valid one and that it's in the proper format
return true;
} // end isValidDate
Do it as below:
String s1 = "01/02/2012";
String s2 = "dd/MM/yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(s2);
try {
Date date = simpleDateFormat.parse(s1);
System.out.println(simpleDateFormat.format(date));
System.out.println("Parse successful. s1 matches with s2");
} catch (ParseException e) {
System.out.println("Parse failed. s1 differs by format.");
}
Please Note: a little warning
if you have s1="01/13/2012" parse will get successful, albeit it is not correct, because it will consider it as "01/01/2013" instead. So if you are ok with this, then proceed, else go ahead with your own implementation.