I am getting date like DDMMYYYYHHMMSS
.
I want to check weather it is valid date
format.
How can I do so?
I am getting date like DDMMYYYYHHMMSS
.
I want to check weather it is valid date
format.
How can I do so?
You can try the below code
public static boolean isValidDate(String inDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
dateFormat.setLenient(false);
try {
dateFormat.parse(inDate.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
Change the format according to your need. Check this link for more details
Pass Any Valid Date Format and check if the date is valid or not
public boolean isThisDateValid(String dateToValidate, String dateFromat){
if(dateToValidate == null){
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
sdf.setLenient(false);
try {
//if not valid, it will throw ParseException
Date date = sdf.parse(dateToValidate);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
return true;
}