Why is this calendar not working?

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); //This call will pass cal.getTime(); } 

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); //This call will fail cal.getTime(); } 

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 
+4
source share
2 answers

This happened because on January 1, Kirimati suffered a 24-hour shift. The Republic of Kiribati, in the Central Pacific, introduced a change of date for its eastern half on 1 January 1995, from time zones UTC−11 and UTC−10 to UTC+13 and UTC+14. Before this, the country was divided by the IDL. After the change, the IDL in effect moved eastwards to go around this country. http://en.wikipedia.org/wiki/International_Date_Line#Eastern_Kiribati .

Also, I took a look at Apia, and it seems that: At 0000 local 30 December 2011 the clocks were moved 24 hours ahead http://en.wikipedia.org/wiki/Apia .

Thus, it is obvious that these two dates do not actually exist on the calendar in their respective time zones due to a 24-hour shift. This may be true for everyone else.

+1
source

EDIT: I initially thought Java did not recognize the meaning of "Pacific / Kiritimati." But it turns out that it is. To get a list of all the strings that Java recognizes, use:

 Set<String> ids = new TreeSet<String>(); for (String id : TimeZone.getAvailableIDs()) ids.add(id); for (String id : ids) System.out.println(id); 
0
source

All Articles