Java timezone conversion example

Java examples to convert a date from one timezone to another timezone. We will see the examples using ZonedDateTime, Date and Calendar classes.

1) Convert ZonedDateTime between timezones

In Java 8, date and time with timezone information is represented with ZonedDateTime.

ZonedDateTime = LocalDateTime + ZoneId

To convert date from one timezone to another, do it in two steps:

  1. Create ZonedDateTime in 1st timezone
  2. Convert above ZonedDateTime in 2nd timezone
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main 
{
	private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a z";
	private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
	
	public static void main(String[] args)
	{
		ZoneId fromTimeZone = ZoneId.of("Asia/Kolkata");	//Source timezone
		ZoneId toTimeZone = ZoneId.of("America/New_York");	//Target timezone
		
		LocalDateTime today = LocalDateTime.now();			//Current time
		
		//Zoned date time at source timezone
		ZonedDateTime currentISTime = today.atZone(fromTimeZone);		
		
		//Zoned date time at target timezone
		ZonedDateTime currentETime = currentISTime.withZoneSameInstant(toTimeZone);
		
		//Format date time - optional
		System.out.println(formatter.format(currentISTime));
		System.out.println(formatter.format(currentETime));
	}
}

//Output:

14-7-2018 11:57:46 PM IST
14-7-2018 02:27:46 PM EDT

Read More: Convert Date to EST/EDT Timezone

2) Convert Date between timezones

java.util.Date represents a time instant without timezone information. By default, if you print timezone information, it will always print the system’s current timezone.

If you have a date, and wants to get same date value in different timezone, then you should use SimpleDateFormat class which is used to format a date in timezone sensitive manner.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main 
{
	static SimpleDateFormat FORMATTER = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mma z");
	
	public static void main(String[] args)
	{
		TimeZone etTimeZone = TimeZone.getTimeZone("America/New_York");	//Target timezone
		
		Date currentDate = new Date();
		
		System.out.println(FORMATTER.format(currentDate));	//Date in current timezone
		
		FORMATTER.setTimeZone(etTimeZone);
		
		System.out.println(FORMATTER.format(currentDate));	//Date in target timezone
	}
}

//Output:

07/15/2018 at 12:13AM IST
07/14/2018 at 02:43PM EDT

3) Convert Calendar between timezones

Internally, Calendar class uses Date class so it also essentially looses any timezone information associated with it; and always default to system’s current timezone.

So just like Date class, you will need to rely of SimpleDateFormat class to add any timezone information. Also, you can use Calendar’s static methods to get timezone impacted date fields.

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

public class Main 
{
	static SimpleDateFormat FORMATTER = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mma z");
	
	public static void main(String[] args)
	{
		TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");	//Source timezone
		TimeZone etTimeZone = TimeZone.getTimeZone("America/New_York");	//Target timezone
		
		Calendar today = Calendar.getInstance(istTimeZone);
		
		System.out.println(FORMATTER.format(today.getTime()));	//07/15/2018 at 12:35AM IST
		
		//Change timezone in formatter
		FORMATTER.setTimeZone(etTimeZone);			
		
		System.out.println(FORMATTER.format(today.getTime()));	//07/14/2018 at 03:05PM EDT
		
		//OR calendar static methods
		
		System.out.println(today.get(Calendar.HOUR));		//0
		System.out.println(today.get(Calendar.MINUTE));		//37
		System.out.println(today.get(Calendar.SECOND));		//9
		
		//Change calendar timezone
		today.setTimeZone(etTimeZone);
		
		System.out.println(today.get(Calendar.HOUR));		//3
		System.out.println(today.get(Calendar.MINUTE));		//7
		System.out.println(today.get(Calendar.SECOND));		//9
	}
}

Drop me your questions in comments section.

Happy Learning !!

References:

  1. IST to ET Conversion
  2. ZonedDateTime Javadoc
  3. Calendar JavaDoc
  4. Date JavaDoc
  5. SimpledateFormat JavaDoc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.