This question is raised by the following code:
DateFormat DF = new SimpleDateFormat("yyyyMMdd");
String dateString = "20110133";
System.out.println(DF.parse(dateString));
// Wed Feb 02 00:00:00 CET 2011
The parse
method transformed Jan 33 to Feb 02.
Is there a way to throw an Exception if the dateString
does not represent a real date?
Just like DateTime.ParseExact in .NET.
Try doing this
DF.setLenient(false);
javadoc reference
You can use the setLenient(boolean)
method of the DateFormat class to tell it not to be lenient (i.e. accept and then convert) with dates that aren't valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With