Tim Pigden Tim Pigden - 1 year ago 176
Java Question

how to convert from ZonedDateTime to Joda DateTime

I've switched to threeten for date times but I've still got a 3rd party tool that uses joda to write timestamp with timezone to the database and I need to convert from one to the other.
What's the best way?
As a workaround I tried DateTime.parse(zdt.toString) but it falls over because joda doesn't like the zone format

Invalid format: "2015-01-25T23:35:07.684Z[Europe/London]" is malformed at "[Europe/London]"

Answer Source
ZonedDateTime zdt = 
    2015, 1, 25, 23, 35, 7, 684000000, 

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download