Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It seems like you're trying to get from a specific local time to a <code>DateTime</code> instance and you want that to be robust against daylight savings. Try this... (note I'm in US/Eastern, so our transition date was 13 Mar 11; I had to find the right date to get the exception you got today. Updated my code below for CET, which transitions today.) The insight here is that Joda provides <a href="http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalDateTime.html" rel="noreferrer"><code>LocalDateTime</code></a> to let you reason about a local wall-clock setting and whether it's legal in your timezone or not. In this case, I just add an hour if the time doesn't exist (your application has to decide if this is the right policy.)</p> <pre><code>import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.LocalDateTime; class TestTz { public static void main(String[] args) { final DateTimeZone dtz = DateTimeZone.forID("CET"); LocalDateTime ldt = new LocalDateTime(dtz) .withYear(2011) .withMonthOfYear(3) .withDayOfMonth(27) .withHourOfDay(2); // this is just here to illustrate I'm solving the problem; // don't need in operational code try { DateTime myDateBorken = ldt.toDateTime(dtz); } catch (IllegalArgumentException iae) { System.out.println("Sure enough, invalid instant due to time zone offset transition!"); } if (dtz.isLocalDateTimeGap(ldt)) { ldt = ldt.withHourOfDay(3); } DateTime myDate = ldt.toDateTime(dtz); System.out.println("No problem: "+myDate); } } </code></pre> <p>This code produces:</p> <pre> Sure enough, invalid instant due to time zone offset transition! No problem: 2011-03-27T03:00:00.000+02:00 </pre>
 

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