0

I want to convert wall clock time time from one TZ to another without doing OFFSET math myself.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
    String d = sdf.format(new Date());
    System.out.println(d);
    sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    String d1 = sdf.format(new Date());
    System.out.println(d1);

Output:

2018.07.09 13:43:30 PDT
2018.07.09 16:43:30 EDT

Desired Output

2018.07.09 13:43:30 PDT
2018.07.09 13:43:30 EDT

How can I get the desired output?

Nuwan Alawatta
  • 1,812
  • 21
  • 29
GJain
  • 5,025
  • 6
  • 48
  • 82
  • 2
    Any reason why you can't use the newer date/time API? – MadProgrammer Jul 09 '18 at 20:55
  • @MadProgrammer As of now, we use Java 7. We have not upgraded our infra to java 8 if you are referring to ZonedDateTime. – GJain Jul 09 '18 at 20:57
  • 1
    So your desired output is supposed to be two _different_ moments in time? Slightly tricky in Java 7 (without Joda time or the new Java 8 date/time API). – Dawood ibn Kareem Jul 09 '18 at 21:01
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Available for Java (6 and) 7 in [the ThreeTen Backport library](http://www.threeten.org/threetenbp/). – Ole V.V. Jul 10 '18 at 08:21

1 Answers1

1

java.time and ThreeTen Backport

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss z", Locale.US);

    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
    String d = zdt.format(formatter);
    System.out.println(d);

    zdt = zdt.withZoneSameLocal(ZoneId.of("America/New_York"));
    String d1 = zdt.format(formatter);
    System.out.println(d1);

Output when I ran the code just now:

2018.07.10 04:30:20 PDT
2018.07.10 04:30:20 EDT

The ZonedDateTime class that you mentioned in a comment has your desired conversion built in, in its withZoneSameLocal method. This returns the same wall clock time in the specified time zone.

As of now, we use Java 7. We have not upgraded our infra to java 8…

No big problem. java.time and its ZonedDateTime work nicely on Java 7. They just require at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • But there is no toInstant method for Date in java 7. How do I connvert a java.util.Date to ZoneDateTime – GJain Jul 13 '18 at 00:53
  • Hi, when you use the ThreeTen Backport library mentioned, conversion from `Date` to `Instant` is done with `DateTimeUtils.toInstant(yourJavaUtilDate)`. – Ole V.V. Jul 13 '18 at 07:50