Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timezone conversion

I need to convert from one timezone to another timezone in my project.

I am able to convert from my current timezone to another but not from a different timezone to another.

For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.

However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.

like image 609
Senthilnathan Avatar asked Jul 04 '11 06:07

Senthilnathan


People also ask

How do you convert TimeZone to time?

(GMT-5:00) Eastern Time (US & Canada)Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11.

What is ze8 time zone?

Hong Kong Time – HKT Time Zone (Standard Time) Currently observing HKT. Areas with same time currently (UTC +8).

What is the formula for calculating time zones?

To find the time zone in hours of a particular location, you can take the longitude -- in degrees -- and divide it by 15.


1 Answers

tl;dr

ZonedDateTime.now( ZoneId.of( "Pacific/Auckland" ))              // Current moment in a particular time zone.              .withZoneSameInstant( ZoneId.of( "Asia/Kolkata" ))  // Same moment adjusted into another time zone.  

Details

The java.util.Date class has no time zone assigned, yet it's toString implementation confusingly applies the JVM's current default time zone.

Avoid java.util.Date & .Calendar

This is one of many reasons to avoid the notoriously troublesome java.util.Date, .Calendar, and SimpleDateFormat classes bundled with Java. Avoid them. Instead use either:

  • The java.time package built into Java 8 and inspired by Joda-Time.
  • Joda-Time

java.time

Java 8 and later has the java.time package built-in. This package was inspired by Joda-Time. While they share some similarities and class names, they are different; each has features the other lacks. One notable difference is that java.time avoids constructors, instead uses static instantiation methods. Both frameworks are led by the same man, Stephen Colbourne.

Much of the java.time functionality has been back-ported to Java 6 & 7 in the ThreeTen-Backport project. Further adapted to Android in the ThreeTenABP project.

In the case of this Question, they work in the same fashion. Specify a time zone, and call a now method to get current moment, then create a new instance based on the old immutable instance to adjust for time zone.

Note the two different time zone classes. One is a named time zone including all the rules for Daylight Saving Time and other such anomalies plus an offset from UTC while the other is only the offset.

ZoneId zoneMontréal = ZoneId.of("America/Montreal");  ZonedDateTime nowMontréal = ZonedDateTime.now ( zoneMontréal );  ZoneId zoneTokyo = ZoneId.of("Asia/Tokyo");  ZonedDateTime nowTokyo = nowMontréal.withZoneSameInstant( zoneTokyo );  ZonedDateTime nowUtc = nowMontréal.withZoneSameInstant( ZoneOffset.UTC ); 

Joda-Time

Some example code in Joda-Time 2.3 follows. Search StackOveflow for many more examples and much discussion.

DateTimeZone timeZoneLondon = DateTimeZone.forID( "Europe/London" ); DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" ); DateTimeZone timeZoneNewYork = DateTimeZone.forID( "America/New_York" );  DateTime nowLondon = DateTime.now( timeZoneLondon ); // Assign a time zone rather than rely on implicit default time zone. DateTime nowKolkata = nowLondon.withZone( timeZoneKolkata ); DateTime nowNewYork = nowLondon.withZone( timeZoneNewYork ); DateTime nowUtc = nowLondon.withZone( DateTimeZone.UTC );  // Built-in constant for UTC. 

We have four representations of the same moment in the timeline of the Universe.


Actually the java.util.Date class does have a time zone buried within its source code. But the class ignores that time zone for most practical purposes. So, as shorthand, it’s often said that j.u.Date has no time zone assigned. Confusing? Yes. Avoid the mess that is j.u.Date and go with Joda-Time and/or java.time.

like image 97
Basil Bourque Avatar answered Oct 05 '22 09:10

Basil Bourque