You may come across a scenario where you need to convert date & time between different time zones in Java. For example in a flight application if you need to derive the arrival time of the flight in a different time zone.
In this post we’ll see how it can be done using the new Data and Time API in Java (Java 8 onward) which has classes like ZoneId and ZonedDateTime for that purpose.
If you are using Java 6 or 7 you can have a look at ThreeTen Backport http://www.threeten.org/threetenbp/ which provides a backport of the Java SE 8 date-time classes to Java SE 6 and 7.
Converting date & time between different time zones Java Example
For this example, I am taking a scenario where flight departs from NewArk (USA) and arrives at New Delhi (India). Departure time is 14:15 PM and date is Aug 28th, 2017. Total flight time is 19 Hrs. 25 Minutes. Using that information, you need to get the arrival time in New Delhi.
If you go by total flight time and the departure time then the arrival time would be 09:40 AM on Aug 29th, 2017. But you’ll need to consider the time zone and time difference between these zones too.
Steps for converting date and time between different time zones are as follows-
- Create Zone Ids for the two time-zones.
- Then create a LocalDateTime object with the departure date and time information.
- Using that create a ZonedDateTime for the departing ZoneId.
- Then get the ZonedDateTime for the arriving ZoneID for the same instant (New_York) in the different time-zone. This gives you the departure time in the arriving ZoneId (Delhi).
- Now just add the total flight time to it in order to get the arrival time.
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class ZonedDate { public static void main(String[] args) { //Getting zone Ids ZoneId newArk = ZoneId.of("America/New_York"); ZoneId delhi = ZoneId.of("Asia/Kolkata"); LocalDateTime dateTime = LocalDateTime.of(2017, 8, 28, 14, 15); System.out.println("Date time " + dateTime); ZonedDateTime newArkDateTime = ZonedDateTime.of(dateTime, newArk); System.out.println("Date time - NewArk " + newArkDateTime); // Getting same time in different time zone ZonedDateTime delhiDateTime = newArkDateTime.withZoneSameInstant(delhi); System.out.println("Date time - India " + delhiDateTime); // Total flight time - 19 Hrs. 25 Mins. converted to mins. ZonedDateTime arrivalTime = delhiDateTime.plusMinutes(1165); System.out.println("Arrival Date time - Delhi " + arrivalTime); } }
Output
Date time 2017-08-28T14:15 Date time - NewArk 2017-08-28T14:15-04:00[America/New_York] Date time - India 2017-08-28T23:45+05:30[Asia/Kolkata] Arrival Date time - Delhi 2017-08-29T19:10+05:30[Asia/Kolkata]
Converting date between different time zones- Day light saving consideration
The above code will take care of the day light saving too. You can change the month to November to verify (Day light saving ends on November 5).
LocalDateTime dateTime = LocalDateTime.of(2017, 11, 28, 14, 15);
With that date if we execute the code output is–
Output
Date time 2017-11-28T14:15 Date time - NewArk 2017-11-28T14:15-05:00[America/New_York] Date time - India 2017-11-29T00:45+05:30[Asia/Kolkata] Arrival Date time - Delhi 2017-11-29T20:10+05:30[Asia/Kolkata]
You can see the one hour difference in arrival now as day light saving is not observed in India.
That's all for this topic Convert Date And Time Between Different Time-Zones in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Programs Page
Related Topics
You may also like-
No comments:
Post a Comment