简体   繁体   中英

SimpleDateFormat bug or misunderstanding pattern and lenient?

Having code:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM d HH:mm yyyy");

This will work as expected with:

simpleDateFormat.parse("Mar 27 02:02 2016");
simpleDateFormat.parse("Mar 27 03:02 2016");

However simpleDateFormat.setLenient(false);

will make the parse("Mar 27 02:02 2016") fail. This makes me totally confused - What I can see, The SDF-format pattern is correct right? So it shouldn't matter if lenient was set. Also, I tried a lot of other random (thousands) datestrings in the same format and it's only "Mar 27 02:02 2016" datestring that fails -. What is going on here? Note, locale is sv_SE if that matters and timezone is "Europe/Stockholm" (and useDaylight for created calendar is true).

I have tested as below and it worked fine for me

Locale locale = new Locale("sv", "SE");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM d HH:mm yyyy", locale);
simpleDateFormat.setLenient(false);
Date pDate = simpleDateFormat.parse("Mar 27 02:02 2016");
//simpleDateFormat.parse("Mar 27 03:02 2016");
System.out.println(pDate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM