Blankman
Blankman

Reputation: 267290

Convert unix time/utc to a specific timezone

I have the current milliseconds utc/unix time and now I want to convert this to a datetime for a specific timezone e.g. EST UTC -5 (new york).

I get the current milliseconds/ unix time using:

System.currentTimeMillis

Now I want to convert this long value to a specific timezone.

How can I do this with Joda time as I have heard this is the best library to use but the API seems a bit confusing to me.

Upvotes: 1

Views: 1136

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 340070

I get the current milliseconds/ unix time using: System.currentTimeMillis

Do not do this.

The Instant.now() method replaces that outmoded method. Furthermore, Instant is capable of finer granularity, up to nanoseconds. Currently the implementation in OpenJDK captures the current moment in microseconds, much finer than that other method’s milliseconds.

Instant instant = Instant.now() ;

instant.toString(): 2019-07-27T02:15:34.727766Z

Now I want to convert this long value to a specific timezone.

Specify your time zone as a ZoneId. Apply to the Instant to get a ZonedDateTime.

ZoneId z = ZoneId.of( "America/New_York" ) ;  // Or did you have another zone in mind, such as `America/Montreal` ?
ZonedDateTime zdt = instant.atZone( z );

Or, you can skip the Instant. Call ZonedDateTime.now while passing a ZoneId.

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

a specific timezone e.g. EST UTC -5 (new york).

EST is not a true time zone.

Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

How can I do this with Joda time as I have heard this is the best library to use but the API seems a bit confusing to me.

The Joda-Time project is now succeeded by the java.time classes built into Java 8 and later as defined by JSR 310. Android 26 and later has these classes too.

You comment about writing to database. If your database has proper date-time types, use them rather than storing a long integer.

As of JDBC 4.2 we can exchange java.time objects with the database.

A moment such as we have discussed here should be stored in a column of a data type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE (WITH, not WITHOUT!).

Oddly, the JDBC spec requires support only for OffsetDateTime rather than Instant or ZonedDateTime. So, convert.

myPreparedStatement.setObject( … , zdt.toOffsetDateTime() ) ;

Retrieval.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;    // Adjust to a time zone.
Instant instant = zdt.toInstant() ;                 // Adjust to UTC.

What is the difference between OffsetDateTime & ZonedDateTime?

  • An offset-from-UTC is merely a number of hours-minutes-seconds. Nothing more.
  • A time zone is much more. A time zone is a history of past, present, and future changes to the offset used by the people of a particular region.

enter image description here

Upvotes: 0

bobah
bobah

Reputation: 18864

You can use DateTime(long) followed by DateTime.withZone(DateTimeZone). Something like

DateTime dt = new DateTime(millis).withZone(DateTimeZone.forID("Europe/Somewhere");

Upvotes: 1

Related Questions