What is EST/EDT/ET Timezone
Eastern Standard Time(EST) is the easternmost timezone in USA and spans from first Sunday in November to second Sunday in March.
EST is 5 hours behind UTC and so it is also referred as UTC - 5
hours.
EST includes Daylight Saving Time(DST).
So, EST is the timezone in winters.
Eastern Daylight Time(EDT) is also the easternmost timezone in USA and spans from second Sunday in March to the first Sunday in November.
EDT is 4 hours behind UTC and so it is also referred as UTC - 4
hours.
EDT includes Daylight Saving Time(DST)
So, EST is the timezone in summers.
ET or Eastern Time is a combination of EST and EDT. So, ET may mean either EST or EDT, depending on the time of year.
In ET, time changes twice a year as Day Light Savings(DST) begins and ends.
In this article, we will understand 2 different ways to convert a date time in different timezone to EST/EDT timezone with examples
Java Timezone
Java supports two time zone IDs for EST or Eastern Standard Time. They are
1. EST5EDT, which may represent EST
or EDT
since it depends on the time of the year.
2. America/New_York, also represents EST
or EDT
.
To deal with Eastern Time, use of America/New_York is preferred over abbreviations as stated in java docs.
To get all the timezones supported by java, use getAvailableIDs()
method from Timestamp
class.
Converting to ET Timezone
Java 8 introduced a new date time api in java.time package.
Below code converts current date time to corresponding time in ET(EST or EDT).
// 1. get IST time ZonedDateTime currentIST = ZonedDateTime.now(); // 2. get current time in ET ZonedDateTime currentET = currentIST.withZoneSameInstant( ZoneId.of("America/New_York")); // 3. IST or general formattter DateTimeFormatter istFormatter = DateTimeFormatter. ofPattern("MM/dd/yyyy hh:mm:ssa"); // 4. ET formatter DateTimeFormatter etFormatter = DateTimeFormatter. ofPattern("MM/dd/yyyy hh:mm:ssa 'ET'"); // 5. format to IST String istTime = istFormatter.format(currentIST); // 6. format to ET String etTime = etFormatter.format(currentET); System.out.println(istTime); System.out.println(etTime);
First, we get the current time. This will be in Indian Standard Time.
Using withZoneSameInstant()
, we get the corresponding time in ET, using America/New_York
timezone.
Next, we create formatters to parse IST and ET date time values determined in earlier steps.
Notice ‘ET’ in ET formatter. This is used to display ET at the end of result and has nothing to do with conversion.
Finally, the date time values are formatted as per these formatters.
Output will be
06/28/2022 11:48:50pm
06/28/2022 02:18:50pm ET
Below is a code that uses
java.util.Date
and java.util.Calendar
classes to convert timezone to ET. // 1. formatter for ET time SimpleDateFormat etFormatter = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ssa 'ET'"); // 2. define target timezone TimeZone timeZone = TimeZone.getTimeZone("America/New_York"); // 3. set formatter timezone etFormatter.setTimeZone(timeZone); // 4. get current date Date date = new Date(); // 5. format date to ET String etFormat = etFormatter.format(date); System.out.println(etFormat); // 6. formatter for IST SimpleDateFormat istFormatter = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ssa"); // 7. format date to IST String istFormat = istFormatter.format(date); System.out.println(istFormat);
In this example, we create a formatter for formatting output date time and set its timezone to ET.
Then, we get the current date using Date
constructor and convert it using this formatter.
Finally, the same date is formatted in the current timezone(which is IST) to show the difference in both timezones.
Output is
06/28/2022 02:25:31pm ET
06/28/2022 11:55:31pm
We can use the same formatter object by formatting the date in IST first and then setting its timezone to ET.
Note that if you can use java 8, then go with recommended first method.
If you try to convert date time to ED timezone, using EDT as timezone id as Timezone.getTimeZone("EDT")
, you will get an incorrect result, since there is no such timezone id supported by java.
There will be no error but the value will be incorrect.
So, either use America/New_York or EST5EDT.