SimpleDateFormat exception when setting lenient to false

Why does this code throw an unpaired date exception?

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 great when condescending. This works strangely for the input date " 2012-03-01T02: 46: 01.000Z ", even with a soft value of false. The default time zone is used: PST

+8
java datetime formatting
source share
1 answer

Since this time does not exist in your time zone by default - it was daylight saving time, and the time jumped from 2:00 to 3:00 in the morning, so that morning was not 2:46.: P

Since you parsed UTC, set the time zone of the SimpleDateFormat instance to UTC as follows:

 f.setTimeZone(TimeZone.getTimeZone("UTC")); 

and your problem will disappear.

+10
source share

All Articles