Java convert date to EST/EDT timezone

Java supports three timezone constants for Eastern Standard Time i.e. "EST", "America/New_York" and "EST5EDT". It is very important to understand difference between them to correctly utilize these constants for converting date or time in Eastern Standard Time values.

1. Difference between EST, EST5EDT and ‘America/New_York’

In the eastern part of the USA, timezone offsets are different during summer and winter.

  1. During winter, time is EST. EST is always UTC-5 hours and without consideration for DST (daylight savings time).
  2. During summer, time is EDT. EDT is UTC-4 hours and with DST.
  3. To correctly represent time, during whole year, you should call it ET (Eastern Time) which include EST and EDT both.
  4. From timezone perspective, EST5EDT means either in EST or EDT. It specifies that the zone uses a standard time of UT-5h called “EST”, a DST of UT-4h called “EDT”, and switches between them annually on.
  5. The time zone America/New_York is the same as EST5EDT for all dates after the ‘Uniform Time Act of 1966‘.
  6. So if you are not using dates before 1966 in your application, then you should use America/New_York timezone. It is preferred way.

Always, prefer to use 'America/New_York' for Eastern time. And use ‘ET’ in formatted timestamp. It represents EST and EDT both.

2. Convert timestamp to ET timezone

Java program to convert date to EST or EDT timezones.

2.1. LocalDateTime [JDK-Version >= Java 8]

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

public class Main 
{
	static DateTimeFormatter globalFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mma z");
	static DateTimeFormatter etFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mma 'ET'");
	
	static ZoneId istZoneId = ZoneId.of("Asia/Kolkata");
	static ZoneId etZoneId = ZoneId.of("America/New_York");
	
	public static void main(String[] args) 
	{		
		LocalDateTime currentDateTime = LocalDateTime.now();
		
		ZonedDateTime currentISTime = currentDateTime.atZone(istZoneId);				//India Time
		ZonedDateTime currentETime = currentISTime.withZoneSameInstant(etZoneId);		//ET Time
		
		System.out.println(globalFormat.format(currentISTime));
		System.out.println(etFormat.format(currentETime));
	}
}

Watch the output:

07/13/2018 at 03:07PM IST
07/13/2018 at 05:37AM ET

2.2. Date and Calendar [JDK-Version < Java 8]

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

public class Main 
{
	public static void main(String[] args) 
	{
		SimpleDateFormat istDf = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mma z");
		SimpleDateFormat etDf = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mma 'ET'");
		TimeZone etTimeZone = TimeZone.getTimeZone("America/New_York");

		//Set ET timezone
		etDf.setTimeZone( etTimeZone );

		Date currentDate = new Date();
		Calendar currentTime = Calendar.getInstance();
		
		//In India Time
		System.out.println(istDf.format(currentDate.getTime()));
		System.out.println(istDf.format(currentTime.getTimeInMillis()));
		
		//In ET Time
		System.out.println(etDf.format(currentDate.getTime()));
		System.out.println(etDf.format(currentTime.getTimeInMillis()));
	}
}

Watch the output:

07/13/2018 at 02:34PM IST
07/13/2018 at 02:34PM IST

07/13/2018 at 05:04AM ET
07/13/2018 at 05:04AM ET

Drop me your questions in the comments section regarding convert date to EST in Java.

Happy Learning !!

Ref:

SO Thread
perl.datetime

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.

7 thoughts on “Java convert date to EST/EDT timezone”

  1. Great example but not thread safe friendly, the SimpleDateFormat doesn’t like concurrency and should not be initialized as a static variable, better to wrap in a method.
    Thanks!

    Reply
  2. A quick question, if we do not use ‘ET’ but use “America/New_York”, will the value be same? In otherwords, the value will automatically use EST and EDT value according to the date-time.

    (“MM/dd/yyyy ‘at’ hh:mma ‘ET'”)

    def ETTestingValue = new Date().format(“yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”, TimeZone.getTimeZone(“America/New_York”));

    Reply
  3. Wed, May 22, 2019 at 19:24:12:564 EDT: printing an integer: 15
    Wed, May 22, 2019 at 19:24:12:564 EDT: printing a boolean: true
    Wed, May 22, 2019 at 19:24:12:564 EDT: printing a character: c

    how to get this output in java

    Reply
  4. Great, but what if you don’t know where you are? I try using TimeZone.getDefault(), but that shows the wrong time when xDT. If I use as you say, it works, but it relies on knowing where you are. The machine is set to a locale, it seems like we should be able to access that.

    Thanks, I have been looking for a solution to this for a while (pre-java-8) and this is close.

    Reply
  5. Daylight Savings Time (DST) refers to savings that can be made during Summer due to more daylight.

    You mixed between EST (Eastern Standard Time) and EDT (Eastern Daylight Time) :
    * EST (UTC-05:00) is used during Winter
    * EDT (UTC-04:00) is used during Summer.

    Reply

Leave a Comment

HowToDoInJava

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