Java supports three timezone constants for Eastern Standard Time i.e. "EST", "America/New_York" and "EST5EDT". It is very important to understand the 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.
- During winter, time is
EST. EST is alwaysUTC-5hours and without consideration for DST (daylight savings time). - During summer, time is
EDT. EDT isUTC-4hours and with DST. - To correctly represent time, during whole year, we should call it
ET (Eastern Time)which includeESTandEDTboth. - From timezone perspective,
EST5EDTmeans either inESTorEDT. It specifies that the zone uses a standard time ofUT-5hcalled “EST”, a DST ofUT-4hcalled “EDT”, and switches between them, annually. - The time zone
America/New_Yorkis the same asEST5EDTfor all dates after the ‘Uniform Time Act of 1966‘. - So if we are not using dates before 1966 in our application, then we should use
America/New_Yorktimezone. It is preferred way.
Always, prefer to use
'America/New_York'for Eastern time. And use ‘ET’ in formatted timestamp. It representsESTandEDTboth.
2. Convert Date Time to ET Timezone
Let us see how to convert a given date-time to an instant in the ET timezone.
2.1. ZonedDateTime
Java program to convert ZonedDateTime in ET timezone.
DateTimeFormatter globalFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mma z");
DateTimeFormatter etFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mma 'ET'");
ZonedDateTime currentISTime = ZonedDateTime.now(); // "Asia/Kolkata"
ZonedDateTime currentETime = currentISTime
.withZoneSameInstant(ZoneId.of("America/New_York")); //ET Time
System.out.println(globalFormat.format(currentETime));
System.out.println(etFormat.format(currentETime));
Watch the output:
02/16/2022 at 08:27am GMT-05:00
02/16/2022 at 08:27am ET
2.2. java.util.Date and Calendar
Java program to print Date in ET timezone.
SimpleDateFormat etDf = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mma 'ET'");
TimeZone etTimeZone = TimeZone.getTimeZone("America/New_York");
etDf.setTimeZone( etTimeZone );
Date currentDate = new Date();
Calendar currentTime = Calendar.getInstance();
//In ET Time
System.out.println(etDf.format(currentDate.getTime()));
System.out.println(etDf.format(currentTime.getTimeInMillis()));
Watch the output:
02/16/2022 at 08:27am ET
02/16/2022 at 08:27am ET
Drop me your questions in the comments section regarding converting the date to EST in Java.
Happy Learning !!
System.out.println(etDf.format(currentDate.getTime()));
you are printing a string.
it will look correct when it is string but when you try to parse this string into java Date object, it will ignore the timezone and create a Date from systems timezone
That is expected.
HI, what do you mean by this So if we are not using dates before 1966 in our application? I recently run into an issue with a date 1949. And I’m getting uncategorized SQLException for SQL [{call odc_list_accountOpeningRequest_full(?, ?, ?, ?)}]; SQL sta te [S1009]; error code [0]; HOUR_OF_DAY: 0 -> 1; nested exception is java.sql.SQLException: HOUR_OF_DAY: 0 -> 1,
If the date is before 1966 what would be the recommended fix?
Thanks
Can you please create and share a simple Java program to reproduce the error? Just to mention, I do not have firsthand experience of handling dates before 1970 or 1966.
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”));
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
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.
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.
Thank you for pointing this out. You are correct. I fixed the mistake.