Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think a lot of the time, you will want joda to fix this automatically for you. You will often not know the correct way to fix a gap date because the size of the gap depends on the zone and the year (although of course it's usually an hour).</p> <p>One example of this is if you are parsing a timestamp that comes from a source you don't control; eg, the network. If the sender of the timestamp has outdated zone files, this could happen. (If you have outdated zone files, you're pretty much screwed).</p> <p>Here's a way to do that, which, granted, is slightly more complicated. I've made it work in joda 1.6 as well as 2.x, since we happen to be stuck on 1.6 in our environment.</p> <p>If you're building a date from some other inputs as in your question, you can start with a UTC date or a <code>LocalDate</code> as suggested above, and then adapt this to automatically fix your offset. The special sauce is in <code>DateTimeZone.convertLocalToUTC</code></p> <p>Dangerous:</p> <pre><code>public DateTime parse(String str) { formatter.parseDateTime(gapdate) } </code></pre> <p>Safe:</p> <pre><code>public DateTime parse(String str) { // separate date from zone; you may need to adjust the pattern, // depending on what input formats you support String[] parts = str.split("(?=[-+])"); String datepart = parts[0]; DateTimeZone zone = (parts.length == 2) ? DateTimeZone.forID(parts[1]) : formatter.getZone(); // parsing in utc is safe, there are no gaps // parsing a LocalDate would also be appropriate, // but joda 1.6 doesn't support that DateTime utc = formatter.withZone(DateTimeZone.UTC).parseDateTime(datepart); // false means don't be strict, joda will nudge the result forward by the // size of the gap. The method is somewhat confusingly named, we're // actually going from UTC to local long millis = zone.convertLocalToUTC(utc.getMillis(), false); return new DateTime(millis, zone); } </code></pre> <p>I've tested this in the eastern and western hemispheres as well as the Lord Howe Island zone, which happens to have a half hour dst.</p> <p>It would be kind of nice if joda formatters would support a setStrict(boolean) that would have them take care of this for you...</p>
 

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