Simplest way to get local milliseconds in a time zone with Joda-Time

Alexey Romanov picture Alexey Romanov · Jul 26, 2012 · Viewed 37k times · Source

Currently, to get milliseconds from start of 1970 in a local time zone, I do

long localMillis = dateTime.withZone(timeZone).toLocalDateTime()
    .toDateTime(DateTimeZone.UTC).getMillis();

This works, but is there a simpler way to do this?

Answer

Andrew McNamee picture Andrew McNamee · Jul 30, 2012

You can make this a little clearer by storing a constant LocalDateTime referring to Jan 1, 1970, and then calculating a Duration between that point in time (for a given time zone) and the instant that you care about, like:

private static final LocalDateTime JAN_1_1970 = new LocalDateTime(1970, 1, 1, 0, 0);

...

new Duration(JAN_1_1970.toDateTime(someTimeZone), endPointInstantOrDateTime).getMillis();