ZonedDateTime zdt = ZonedDateTime.of( 2015, 1, 25, 23, 35, 7, 684000000, ZoneId.of("Europe/London")); System.out.println(zdt); // 2015-01-25T23:35:07.684Z[Europe/London] System.out.println(zdt.getZone().getId()); // Europe/London System.out.println(zdt.toInstant().toEpochMilli()); // 1422228907684 DateTimeZone london = DateTimeZone.forID(zdt.getZone().getId()); DateTime dt = new DateTime(zdt.toInstant().toEpochMilli(), london); System.out.println(dt); // 2015-01-25T23:35:07.684Z
In the event that zone ID conversion may fail for any unsupported or unrecognized identifier, I recommend
- catch and log
- make updates to tz repositories (for Joda: upgrade to the latest version, for JDK: use tz-updater-tool)
This is usually a better strategy than just silence back to any arbitrary tz-offset, like UTC.
source share