Make SimpleDateFormat.parse() fail on invalid dates (e.g. month is greater than 12)
I'm using java.text.SimpleDateFormat
to parse strings of the form "yyyyMMdd"
.
If I try to parse a string with a month greater than 12, instead of failing, it rolls over to the next year. Full runnable repro:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseDateTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date result = format.parse("20091504"); // <- should not be a valid date!
System.out.println(result); // prints Thu Mar 04 00:00:00 CST 2010
}
}
I would rather have a ParseException
thrown.
Is there any non-hacky way of forcing the exception to happen?. I mean, I don't want to manually check if the month is greater than 12. That's kind of ridiculous.
Thanks for any suggestion.
NOTE: I already know about Joda Time, but I need this done in plain JDK without external libraries.
Solution 1:
You need to make it non-lenient. Thus,
format.setLenient(false);
should do it.
Solution 2:
You can use the Java 8 time API. If by example you use a month with a value of 15
:
String strDate = "20091504";
TemporalAccessor ta = DateTimeFormatter.ofPattern("yyyyMMdd").parse(strDate);
You will directly have an exception
Exception in thread "main" java.time.format.DateTimeParseException:
Text '20091504' could not be parsed:
Invalid value for MonthOfYear (valid values 1 - 12): 15
Solution 3:
By using Java 8 LocalDate
you can write like this,
String strDate = "20091504";
LocalDate date1 = LocalDate.parse(strDate, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(date1);
Here is the Exception is thrown during parsing
Exception in thread "main" java.time.format.DateTimeParseException: Text '20091504' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 15
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDate.parse(Unknown Source)
at com.katte.infa.DateFormatDemo.main(DateFormatDemo.java:22)
Caused by: java.time.DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 15
at java.time.temporal.ValueRange.checkValidIntValue(Unknown Source)
at java.time.temporal.ChronoField.checkValidIntValue(Unknown Source)
at java.time.chrono.IsoChronology.resolveYMD(Unknown Source)
at java.time.chrono.IsoChronology.resolveYMD(Unknown Source)
at java.time.chrono.AbstractChronology.resolveDate(Unknown Source)
at java.time.chrono.IsoChronology.resolveDate(Unknown Source)
at java.time.chrono.IsoChronology.resolveDate(Unknown Source)
at java.time.format.Parsed.resolveDateFields(Unknown Source)
at java.time.format.Parsed.resolveFields(Unknown Source)
at java.time.format.Parsed.resolve(Unknown Source)
at java.time.format.DateTimeParseContext.toResolved(Unknown Source)
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
... 3 more