I have a date stored in a string and I need to validate whether it represents a date or weekdate in ISODateFormat.
String is acceptable if it is in either format.
I can build 2 formatters and pass the string and check where they both throw exceptions and verify it.
String date;
final DateTimeFormatter dateFormatter = ISODateTimeFormat.date();
final DateTimeFormatter weekdateFormatter = ISODateTimeFormat.weekDate();
boolean isDate=true,isWeekDate=true;
try {
dateFormatter.parseDateTime(date);
}
catch (IllegalArgumentException e) {
isDate =false;
}
try {
weekdateFormatter.parseDateTime(date);
}
catch (IllegalArgumentException e) {
isWeekDate =false;
}
if(!isDate && !isWeekDate)
throw UserDefinedException();
Is there any better way to do it?