In my project I need to check if a date string evaluates to a proper Date object. I\'ve decided to allow yyyy-MM-dd, and Date formats [(year, month, date) and (year, month,
You can construct SimpleDateFormat objects for your different String formats like this (returning null
if the parameter cannot be parsed as a valid date):
// Initializing possibleFormats somewhere only once
SimpleDateFormat[] possibleFormats = new SimpleDateFormat[] {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy,MM,dd"),
new SimpleDateFormat("yyyy,MM,dd,HH,mm") };
for (SimpleDateFormat format: possibleFormats)
{
format.setLenient(false);
}
// initializing ends
public Date parseDate(String date) {
Date retVal = null;
int index = 0;
while (retVal == null && index < possibleFormats.length) {
try {
retVal = possibleFormats[index++].parse(date);
} catch (ParseException ex) { /* Do nothing */ }
}
return retVal;
}
You are doing this the wrong way. You should use SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date test = sdf.parse(input);
} catch (ParseException pe) {
//Date is invalid, try next format
}
For anyone popping by in 2017 or later, here is the Java 8 solution:
private static DateTimeFormatter[] possibleFormats = {
DateTimeFormatter.ofPattern("uuuu-MM-dd").withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern("uuuu,MM,dd").withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern("uuuu,MM,dd,HH,mm").withResolverStyle(ResolverStyle.STRICT),
DateTimeFormatter.ofPattern("uuuu,MM,dd,HH,mm,ss").withResolverStyle(ResolverStyle.STRICT)
};
public Optional<LocalDate> parseDate(String date) {
for (DateTimeFormatter format : possibleFormats) {
try {
LocalDate result = LocalDate.parse(date, format);
return Optional.of(result);
} catch (DateTimeParseException dtpe) {
// ignore, try next format
}
}
return Optional.empty();
}
With DateTimeFormatter
you can safely let the array of formatters be static even in a multithreaded environment: unlike SimpleDateFormat
, a DateTimeFormatter
is thread safe.
A LocalDate
is a date without a time, so if any hours and minutes were present in the string, they are lost. To get the hours, minutes and seconds out too you need a LocalDateTime
, and then you need some trickery to make the parsing work in the cases where there are no hours and minutes in the string. You may either try both LocalDate.parse()
and LocalDateTime.parse()
, or you may build a formatter that has a default for hours to use when not in the string. DateTimeFormatterBuilder
can do that.
Just to show the final outcome, thanks to Csaba_H and Steve Kuo.
private Date parseDate(String date){
SimpleDateFormat[] possibleFormats = new SimpleDateFormat[] {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy,MM,dd"),
new SimpleDateFormat("yyyy,MM,dd,HH,mm") };
Date retVal = null;
for (SimpleDateFormat f: possibleFormats) {
f.setLenient(false);
try {
retVal = f.parse(date);
} catch (ParseException e) {}
}
return retVal;
}