Java Program to Convert Between Time Zones

In this post we’ll see how to convert date and time between different time zones in Java.

There are two Java programs given here-

1- Converting using the Calendar class if you are not using the Java 8 date and time API.
2- Using the Java 8 Data and Time API which has classes like ZoneId and ZonedDateTime for that purpose.

In the example given here we’ll take the source as Indian time (Zone Id- “Asia/Kolkata”)

Which will be converted to PST (Zone Id- “America/Los_Angeles”)

Converting date and time between different time zones using Java 8 API

Steps for the program to convert date and time between different time zones are as follows-

1- Create two Zone Ids for the two time-zones using the of() method of the java.time.ZoneId class.
2- Create ZonedDateTime instance for the source date-time.
3- Use withZoneSameInstant(ZoneId zone) method of the java.time.ZonedDateTime which returns a copy of this date-time with a different time-zone, retaining the instant. That gives the date-time in the converted time zone.

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ConvertDate {
  public static void main(String[] args) {
    ZoneId IndiaZone = ZoneId.of("Asia/Kolkata");	
    ZoneId CaliforniaZone = ZoneId.of("America/Los_Angeles");	
		  
    String dateTime = "2019-10-20 10:30:45 AM";
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a");
    LocalDateTime ldt = LocalDateTime.parse(dateTime, dtf);
		  
    // Source ZonedDateTime
    ZonedDateTime IndiaTime = ZonedDateTime.of(ldt, IndiaZone);
    System.out.println("India Date-Time- " + IndiaTime);
    DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm:ss a");
    // Formatting as per local datetime format
    System.out.println("India Date-Time(dd/MM/yyyy)- " + dtf1.format(IndiaTime));
    
    // Get the target ZonedDateTime
    ZonedDateTime CaliforniaDateTime = IndiaTime.withZoneSameInstant(CaliforniaZone);
    System.out.println("California Date-time " + CaliforniaDateTime);
    // Formatting as per local datetime format
    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a");
    System.out.println("California Date-Time(MM/dd/yyyy)- " + dtf2.format(CaliforniaDateTime));
  }
}

Output

India Date-Time- 2019-10-20T10:30:45+05:30[Asia/Kolkata]
India Date-Time(dd/MM/yyyy)- 20/10/2019 10:30:45 AM
California Date-time 2019-10-19T22:00:45-07:00[America/Los_Angeles]
California Date-Time(MM/dd/yyyy)- 10/19/2019 10:00:45 PM

As you can see there is a time difference of 12:30 Hours between the two time zones. India is 12 hours and 30 minutes ahead of Los Angeles, CA, USA. Note that Day light saving is taken into consideration by these classes, months during which the day light saving is not observed difference is 11 hours and 30 minutes.

Converting date and time between different time zones using GregorianCalendar

If you are using GregorianCalendar calendar.setTime() method sets the exact time as the source. You can see it in the output too where calendar1.getTime() call displays the same time as source.
Ensure that while formatting using SimpleDateFormat you set the time zone too which converts the date-time to the set time-zone.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class ConvertDate {
  public static void main(String[] args) {		
    // Creating GregorianCalendar with values
    GregorianCalendar calendar = new GregorianCalendar(2019, Calendar.OCTOBER, 20, 10, 30, 45);
    calendar.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    System.out.println("India Date-Time- " + calendar.getTime());
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    // Formatting as per local datetime format
    System.out.println("India Date-Time(dd/MM/yyyy)- " + sdf.format(calendar.getTime()));
    
    GregorianCalendar calendar1 = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"));
    calendar1.setTime(calendar.getTime());
    System.out.println("California Date-time " + calendar1.getTime());
    SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");  
    sdf1.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
    System.out.println("California Date-Time(MM/dd/yyyy)- " + sdf1.format(calendar1.getTime()));      
  }
}

Output

India Date-Time- Sun Oct 20 10:30:45 IST 2019
India Date-Time(dd/MM/yyyy)- 20/10/2019 10:30:45 AM
California Date-time Sun Oct 20 10:30:45 IST 2019
California Date-Time(MM/dd/yyyy)- 10/19/2019 10:00:45 PM

Related Posts

That’s all for the topic Java Program to Convert Between Time Zones. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.