Wednesday, 23 August 2017

How to convert date and time between different time-zones in Java

You may come across a scenario where you need to convert date & time between different time zones.

As 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 Java 8 Data and Time API 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.

Example Code

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.

What you need to do is -

  • 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]

Day light saving

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 How to 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!


Related Topics

  1. How to format date in Java using SimpleDateFormat
  2. How to convert Date to String in Java
  3. How to convert String to Date in Java
  4. How to find last modified date of a file in Java
  5. Converting int to string - Java Program

You may also like -

>>>Go to Java Programs page

1 comment:

  1. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with
    excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.


    SEO Training in Chennai

    ReplyDelete