So, I have a Java application that respects your time zone, and I noticed that some time zones have invalid dates ...
I use SimpleDateFormatter('yyyyMMdd')
for parsing ... and found that it does not work with ParseException
in specific combinations of time and time.
I looked at the insides and it looks like it is failing because the Calendar
class throws an IllegalArgumentException
.
I use setLenient(false)
because I do not want the parsing to make assumptions. By default, setLenient(true)
assumes the absence of fields and does not throw an IllegalArgumentException
.
Why does it work:
public void testCalendarPass() { Calendar cal = Calendar.getInstance(); cal.clear(); cal.setLenient(false); cal.set(Calendar.YEAR, 1995); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DAY_OF_MONTH,1);
So far this one in Pacific / Kiritimati fails:
public void testCalendarFailure() { Calendar cal = Calendar.getInstance(); cal.clear(); cal.setTimeZone(TimeZone.getTimeZone("Pacific/Kiritimati")); cal.setLenient(false); cal.set(Calendar.YEAR, 1995); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DAY_OF_MONTH,1);
Is there January 1, 1995 in Kiritimati? How do they refer to the time that falls into this gap?
Now that I have encountered ParseException
I default to server timezone for parsing. But this introduces an offset of up to 24 hours (depending on the time zone).
Here are other dates that do not work in other time zones:
19930820 FAILS on Kwajalein 19930820 FAILS on Pacific/Kwajalein 20111230 FAILS on MIT 20111230 FAILS on Pacific/Apia 19950101 FAILS on Pacific/Enderbury 20111230 FAILS on Pacific/Fakaofo 19950101 FAILS on Pacific/Kiritimati