Converting date to EST/EDT Timezone in Java

In this tutorial, we are going to learn how to convert from our local time to EST/EDT date and time. This is useful in different situations, for example, when you are contacting a colleague in a different timezone or if you are checking out flights. This requires that you convert a date/time from one timezone to another.

This can be tricky if you’re dealing with timezones that use the Daylight Savings Time (DST) designation for some months of the year. DST is the practice of advancing clocks during the summer months to take advantage of the extra natural light. It is used in many countries across the globe, so it is very important to keep it in mind when working with date/time conversions.

Places in the EST/EDT Timezone use the time offset of UTC-05:00 and UTC-04:00 during standard time and daylight savings time, respectively. This means they are one hour ahead during EDT.

Date/time conversions can be done in several ways i.e.

  • Using the SimpleDateFormat,
  • Using the Three Letter Timezone ID like GMT, EDT, PST.
  • Using the Timezone Database (TZDB) Timezone ID.
  • Using Java’s Time API.

Converting date to a Timezone in Java

When working with dates in Java, it’s important to ensure that they are correctly converted to the desired timezone. There are several ways to achieve this, each with its own advantages and disadvantages. In this tutorial, we’ll explore some of the most common approaches to converting dates to the Eastern Time Zone (ET) in Java, including using the SimpleDateFormat and ZonedDateTime classes.

Using the SimpleDateFormat

To convert a Date object to the Eastern Time Zone (ET) in Java using the SimpleDateFormat class, you can follow these steps:

Step 1. Create a SimpleDateFormat object with the desired output format and timezone:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));

Here, we create a SimpleDateFormat object with the pattern “yyyy-MM-dd HH:mm:ss” (year-month-day hour:minute:second) and set its timezone to “America/New_York“, which is the timezone for the Eastern Time Zone.

Step 2. Use the SimpleDateFormat object to format the Date object:

Date date = new Date();
String estTime = sdf.format(date);
System.out.println("EST Time: " + estTime);

Here, we create a Date object representing the current date and time, then use the SimpleDateFormat object to format it into a string representation of the date and time in the Eastern Time Zone.

This approach can be useful if you need to convert dates to a specific format and timezone for display or storage purposes. However, if you need to perform calculations or comparisons with the date in the Eastern Time Zone, it may be more appropriate to use the ZonedDateTime class instead.

Using the Three Letter Timezone ID like GMT, EDT, PST

This is the worst way to carry out date/time conversions. This method should not be used due to several issues:

  • A timezone ID could mean multiple time zones, and Java would only recognize one. For example, CST could mean China Standard Time or Central Standard Time.
  • It does not work when Daylight Saving Time is involved, so the results would be wrong.

Using the Timezone Database (TZDB) Timezone ID

Using the TZDB Timezone ID is a good way of carrying out date/time conversions. This can be done using the java.util.TimeZone package. There are several ways to use this method.

In one scenario, we can get our local time by using the java.util.Calendar as shown in the code example:

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

public class TimezoneConverter {
    public static void main(String[] args) {
        int sec, min, hr, month, yr, day;
        Calendar local = Calendar.getInstance();
        sec = local.get(Calendar.SECOND);
        min = local.get(Calendar.MINUTE);
        hr = local.get(Calendar.HOUR);
        month = local.get(Calendar.MONTH);
        day = local.get(Calendar.DATE);
        yr = local.get(Calendar.YEAR);

        System.out.printf("Local time: %d/%d/%d %02d:%02d:%02d\n", day, month, yr, hr, min, sec);


        Calendar est_edt= new GregorianCalendar(TimeZone.getTimeZone("America/Toronto"));
        sec = est_edt.get(Calendar.SECOND);
        min = est_edt.get(Calendar.MINUTE);
        hr = est_edt.get(Calendar.HOUR);
        month = est_edt.get(Calendar.MONTH);
        day = est_edt.get(Calendar.DATE);
        yr = est_edt.get(Calendar.YEAR);

        System.out.printf("Toronto time: %d/%d/%d %02d:%02d:%02d\n", day, month, yr, hr, min, sec);
    }
}

In the above example, we are looking at the local time and the time in Toronto, a city that is in the EST/EDT Timezone. We get the local time information and extract the day, month, year, hour, minutes, and seconds. We display it as a string.

We then get the timezone of the other location using java.util.GregorianCalendar. We use the TZDB Timezone ID of “America/Toronto”. Using this timezone id instead of the three-letter timezone id ensures that Java can identify the exact timezone you want to convert to and does not require specifically stating that the area is in EST or EDT. The database has the most recent information about the location that uses the specified TZDB timezone Id.

Using this method, however, is not guaranteed. This is because some java.util packages have compatibility concerns and sometimes don’t refactor correctly. An example can be returning the wrong month or day but the right time associated with the TZDB timezone Id.

Using Java’s Time API

The Java Date/Time API was introduced to address the shortcomings in java.util packages that are used in date/time conversions. This API is used by importing java.time. Instead of using java.util.Date, we use java.time.LocalDateTime.

import java.time.ZoneId;
import java.time.LocalDateTime;

public class TimezoneConverter {
    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.of("America/Toronto");
        System.out.println(LocalDateTime.now()); // local time
        System.out.println(LocalDateTime.now(zoneId)); // time in the location
    }
}

In the example above, we see that the local time can be retrieved with LocalDateTime.now(), which returns the date and time in the format “2023-04-17T12:46:55.077464“. This can be converted to a string for better display.

We can also use java.time.ZonedDateTime to get the date and time of a specified timezone. Please note that the zone id we use is the TZDB timezone id.

You can read more on Java packages here, on converting LocalDateTime to strings here, and on string formatting using print() here.