0

I have a Date field in Java in IST Time. I want to convert the same to EST Time and the output should be as a Date Type only. I am able to accomplish the same using the below piece of code:-

SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
Date date = new Date();
DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String estTime = timeFormat.format(date);
date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(estTime);

The problem with the above piece of code is that though the date is converted in EST Time, the Time Zone is still showing as IST and not EST. The rest of the date is converted perfectly fine. Is there any way to explicitly set the time Zone To EST in the Date Field.Any help regarding this will be highly appreciated.

-Subhadeep

Vegard
  • 4,802
  • 1
  • 20
  • 30
user2182350
  • 1
  • 1
  • 1
  • 2

4 Answers4

1

The Date class is time-zone agnostic. Basically, it is always based on GMT although when it is printed it uses the current system time zone to adjust it.

However, Calendar is time-zone specific. See Calendar.setTimeZone().

Consider:

   Calendar cal = new GregorianCalendar();
   cal.setTimeZone(TimeZone.getTimeZone("America/New_York"));
   cal.setTime(new Date());
John B
  • 32,493
  • 6
  • 77
  • 98
1

java.time

The legacy date-time API (java.util date-time types and their formatting type, SimpleDateFormat etc.) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using java.time, the modern API:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.now();
        System.out.println(instant);

        ZonedDateTime zdtIndia = instant.atZone(ZoneId.of("Asia/Calcutta"));
        ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));

        System.out.println(zdtIndia);
        System.out.println(zdtNewYork);
    }
}

Output:

2021-05-19T21:08:54.241341Z
2021-05-20T02:38:54.241341+05:30[Asia/Calcutta]
2021-05-19T17:08:54.241341-04:00[America/New_York]

Instant represents an instantaneous point on the timeline. The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Learn more about java.time, the modern date-time API* from Trail: Date Time.

Solution using legacy API:

The java.util.Date object is not a real date-time object like the modern date-time types; rather, it represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT (or UTC). When you print an object of java.util.Date, its toString method returns the date-time in the JVM's timezone, calculated from this milliseconds value. If you need to print the date-time in a different timezone, you will need to set the timezone to SimpleDateFormat and obtain the formatted string from it.

Demo:

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

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS[zzzzz]");

        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
        String dtIndia = sdf.format(date);

        sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        String dtNewYork = sdf.format(date);

        System.out.println(dtIndia);
        System.out.println(dtNewYork);
    }
}

Output:

Wed May 19 22:16:08 BST 2021
2021-05-20T02:46:08.024[India Standard Time]
2021-05-19T17:16:08.024[Eastern Daylight Time]

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Should you add z for time-zone pattern in your SimpleDateFormat pattern?

So, it should be DateFormat timeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"). I changed your code like this:

public static void main(String[] args) throws ParseException {
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
    Date date = new Date();

    System.out.println(dateTimeFormat.format(date)); // this print IST Timezone

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

    String estTime = timeFormat.format(date);
    date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z", Locale.ENGLISH).parse(estTime);

    System.out.println(timeFormat.format(date)); // this print EDT Timezone currently (on March)
}

In last print statement, current date format is printed with EDT Timezone (Eastern Daylight Time). Maybe because of this.

Wayan Wiprayoga
  • 4,472
  • 4
  • 20
  • 30
0

The correct answer by John B explains that java.util.Date seems to have a time zone but does not. Its toString method applies your JVM's default time zone when generating the string representation.

That is one of many reasons to avoid java.util.Date and .Calendar classes bundled with Java. Avoid them. Instead use either Joda-Time or the java.time package built into Java 8 (inspired by Joda-Time, defined by JSR 310).

Here is some example code in Joda-Time 2.3.

String input = "01/02/2014 12:34:56";
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "MM/dd/yyyy HH:mm:ss" );
DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = formatterInput.withZone( timeZoneIndia ).parseDateTime( input );

DateTimeZone timeZoneNewYork = DateTimeZone.forID( "America/New_York" );
DateTime dateTimeNewYork = dateTimeIndia.withZone( timeZoneNewYork );

DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );

Dump to console…

System.out.println( "input: " + input );
System.out.println( "dateTimeIndia: " + dateTimeIndia );
System.out.println( "dateTimeNewYork: " + dateTimeNewYork );
System.out.println( "dateTimeUtc: " + dateTimeUtc );

When run…

input: 01/02/2014 12:34:56
dateTimeIndia: 2014-01-02T12:34:56.000+05:30
dateTimeNewYork: 2014-01-02T02:04:56.000-05:00
dateTimeUtc: 2014-01-02T07:04:56.000Z
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154