Joda Time converting time zoned date time to milliseconds

cianBuckley picture cianBuckley · Aug 16, 2013 · Viewed 30.8k times · Source

Can somebody please explain to me why when I try to get the milliseconds of a DateTime with a zone different from my own, it gives me back the milliseconds of my local machine's time zone? I'm looking to get the milliseconds of UTC, but my local settings of the application are set to EST (even though I'm actually in Ireland ;) )

Here's the code:

  DateTimeFormatter format = DateTimeFormat.mediumDateTime();

  DateTime local = new DateTime();
  DateTime utc = new DateTime(System.currentTimeMillis(), DateTimeZone.UTC);
  System.out.println("utc zone = " +utc.getZone());
  System.out.println("UTC time: " + utc);
  long current = utc.getMillis();

  System.out.println("current: " + format.print(current));
  System.out.println("local: " + format.print(local.getMillis()));

Here's what it prints:

 utc zone = UTC
 UTC time: 2013-08-16T13:36:32.444Z
 current: Aug 16, 2013 9:36:32 AM
 local: Aug 16, 2013 9:36:32 AM

Currently, I would have thought it should have the same date time as UTC time?

Answer

Matt Johnson-Pint picture Matt Johnson-Pint · Aug 16, 2013

A few points:

  • You don't need to call System.currentTimeMillis(). Just pass this:
    DateTime utc = new DateTime(DateTimeZone.UTC);
  • When you call .getMillis() the result is always in UTC. Representing time as an integer that is not UTC-based is a bad idea, and not something that Joda Time will provide. Your assessment that the milliseconds are affected by your local time zone is incorrect.

  • The conversion errors are because of how you are calling format.print. When you pass an integer, the default behavior is to use the current time zone, which is you local time. You can change it like this:

    format.withZone(DateTimeZone.UTC).print(current)
  • However, if the intention is to print it as a date string, why go to milliseconds first anyway? If you pass the original DateTime, then its time zone information will be applied automatically.
    format.print(utc)
    format.print(local)

Put together, the whole thing would look like this:

DateTime local = new DateTime();
DateTime utc = new DateTime(DateTimeZone.UTC);

System.out.println("local zone = " + local.getZone());
System.out.println("  utc zone = " + utc.getZone());

DateTimeFormatter format = DateTimeFormat.mediumDateTime();
System.out.println(" local: " + format.print(local));
System.out.println("   utc: " + format.print(utc));
System.out.println("millis: " + utc.getMillis());