Why is this code throwing exception of unparseable date?
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.000Z'");
f.setLenient(false);
String dateStr = "2012-03-11T02:46:01.000Z";
f.parse(dateStr);
It works fine when lenient is true. It strangely works for input date '2012-03-01T02:46:01.000Z' even with lenient as false. Default timezone being used : PST
The setLenient(boolean leniency) method in DateFormat class is used to specify whether the interpretation of the date and time of this DateFormat object is to be lenient or not.
DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.
With lenient interpretation, a date such as "February 942, 1996" will be treated as being equivalent to the 941st day after February 1, 1996. With strict (non-lenient) interpretation, such dates will cause an exception to be thrown. The default is lenient.
Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.
Because that time does not exist in your default time zone—it was daylight savings time change day, and time jumped from 2:00 a.m. to 3:00 a.m., so there was no 2:46 that morning. :P
Since you’re parsing UTC, set the SimpleDateFormat
instance time zone to UTC like so:
f.setTimeZone(TimeZone.getTimeZone("UTC"));
and your problem will go away.
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