Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda Time converting time zoned date time to milliseconds

Tags:

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?

like image 772
cianBuckley Avatar asked Aug 16 '13 13:08

cianBuckley


People also ask

Where does Joda-Time get its time zone data?

Time zone data is provided by the public IANA time zone database. The following table shows all the time zones supported by Joda-Time, using version 2018g of the database. It is also possible to update to a later version of the database.

What is zoneddatetime in Joda?

A ZonedDateTime represents a date-time with a time offset and/or a time zone in the ISO-8601 calendar system. On its own, ZonedDateTime only supports specifying time offsets such as UTC or UTC+02:00, plus the SYSTEM time zone ID. The SYSTEM zone ID is a non-standard ID that is specific to js-joda.

What is @JS-Joda/timezone?

The @js-joda/timezone package provides bindings to the the IANA tz database, making joda-js 's calculations time zone aware. The tz database uses zone names like Africa/Bujumbura, America/New_York, and Europe/Lisbon (see the full list ).

How to convert localdatetime to milliseconds in Java 8?

As a result, the toEpochMilli () method returns the same number of milliseconds as we defined earlier. 3.2. Using LocalDateTime Similarly, we can use Java 8's Date and Time API to convert a LocalDateTime into milliseconds: First, we created an instance of the current date.


1 Answers

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()); 
like image 102
Matt Johnson-Pint Avatar answered Dec 16 '22 07:12

Matt Johnson-Pint