Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this Calendar fail?
    primarykey
    data
    text
    <p>So I have a Java app that respects your timezone and I noticed some timezones have invalid dates...</p> <p>I'm using <code>SimpleDateFormatter('yyyyMMdd')</code> to do the parsing... and found that it fails with a <code>ParseException</code> on certain timezone-date combos.</p> <p>I looked into the internals and it looks like it's failing because the <code>Calendar</code> class is throwing an <code>IllegalArgumentException</code>.</p> <p>I'm using <code>setLenient(false)</code> because I don't want the parsing making assumptions. The default using <code>setLenient(true)</code> assumes the missing fields and does not throw the <code>IllegalArgumentException</code>.</p> <p>Why does this one work:</p> <pre><code> 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(); } </code></pre> <p>While this one in Pacific/Kiritimati fails:</p> <pre><code>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(); } </code></pre> <p>Does January 1, 1995 not exist in Kiritimati? How do they reference time that falls within that gap?</p> <p>For now, when I encounter the <code>ParseException</code> I default to the server's timezone for parsing. But this introduces an offset of up to 24 hours (depending on the timezone).</p> <p>Here's a list of other dates that fail on other timezones:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload