0

I tried the below approach and searched in Web to find the solution for this but no luck : looking for the solution for converting a String in IST to PST:

String string = new Date().toString();
        System.out.println(string);
        SimpleDateFormat dt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        dt.setTimeZone(TimeZone.getTimeZone("PST"));
        Date D = dt.parse(string);
        System.out.println(""+ D);

Even when I set time zone as PST, I am seeing out put in IST here is the out put:

Tue Apr 18 18:58:09 IST 2017 Tue Apr 18 18:58:09 IST 2017

I tried another Option here I am seeing even it is showing the time in PST but I see below output it is a bit confusing:

public static Date convertFromOneTimeZoneToOhter(Date dt,String from,String to ) {

          TimeZone fromTimezone =TimeZone.getTimeZone(from);//get Timezone object
          TimeZone toTimezone=TimeZone.getTimeZone(to);

          long fromOffset = fromTimezone.getOffset(dt.getTime());//get offset
          long toOffset = toTimezone.getOffset(dt.getTime());

          //calculate offset difference and calculate the actual time
          long convertedTime = dt.getTime() - (fromOffset - toOffset);
          Date d2 = new Date(convertedTime);

          return d2;
    }

OUT PUT: Converted Date : Tue Apr 18 06:28:09 IST 2017

Can someone please help on this: I found lot of solutions on converting IST Date time to PST String but not IST/EST Date to PST Date. As I mentioned above we can format to a String, but I am looking for an example of converting back to Date

user7076183
  • 33
  • 1
  • 1
  • 11
  • 4
    You're printing the result of `Date.toString()`. Don't do that. A `Date` object doesn't *have* a time zone - it's just an instance in time. The method `convertFromOneTimeZoneToOhter` is fundamentally flawed in its whole purpose. Use `SimpleDateFormat` to specify the time zone you want for formatting purposes. – Jon Skeet Apr 18 '17 at 13:43
  • 3
    Have you considered using a better date/time API? (because `java.util.Date` is terrible for what you need - actually it's terrible at all). For java <= 1.7, use [Joda-Time](http://www.joda.org/joda-time/) or [ThreeTen backport](http://www.threeten.org/threetenbp/), and for java >= 1.8, use `java.time` classes. Take some time to learn those, it's totally worth it. –  Apr 18 '17 at 13:47
  • @JonSkeet, I used SimpleDateFormat but I can convert it to string only.my requirement is convert Date IST to Date PST. – user7076183 Apr 18 '17 at 13:53
  • 2
    @user7076183: Then you haven't understood my comments. There's no such thing as "Date IST" or "Date PST". A `Date` object only represents an instant in time. – Jon Skeet Apr 18 '17 at 15:13
  • Possible duplicate of [How to convert date time from one time zone to another time zone](http://stackoverflow.com/questions/8238661/how-to-convert-date-time-from-one-time-zone-to-another-time-zone) – Ole V.V. Apr 18 '17 at 19:35
  • My Java seems to interpret IST as Israel Standard Time. Is this OK with you? Asking because it may also mean Irish Standard Time and India Standard Time. And because Israel is on DST on April 18, so I would have expected IDT in the string (Israel Daylight Time). You really should stay away from three-letter time zone abbreviations. – Ole V.V. Apr 18 '17 at 20:18
  • @OleV.V. yeah that's fine, I just need an example for how to convert a date from one time zone to other timezone in Java 6/ java 7, struggling from this morning but no luck :( – user7076183 Apr 18 '17 at 21:04

2 Answers2

4

You should look into Java 8's new Date API that handles timezones directly

// Get the current date and time
      ZonedDateTime date1 = ZonedDateTime.parse("2007-12-03T10:15:30+05:30[Asia/Karachi]");
      System.out.println("date1: " + date1);

      ZonedDateTime zonedDateTime = ZonedDateTime.now();
      System.out.println("Zoned Date Time: " + zonedDateTime);

      ZoneId id = ZoneId.of("Europe/Paris");
      System.out.println("ZoneId: " + id);

      ZoneId currentZone = ZoneId.systemDefault();
      System.out.println("CurrentZone: " + currentZone);

Prints :

date1: 2007-12-03T10:15:30+05:00[Asia/Karachi]
Zoned Date Time: 2017-04-18T09:52:09.045-04:00[America/New_York]
ZoneId: Europe/Paris
CurrentZone: America/New_York
Jake O
  • 64
  • 1
  • Thanks for the suggestion, but we are using java 1.7 :( – user7076183 Apr 18 '17 at 15:57
  • You can use the `java.time` classes with Java 1.7 if you want. You can get them in the [ThreeTen Backport](http://www.threeten.org/threetenbp/). – Ole V.V. Apr 18 '17 at 19:37
  • You may particularly like [`ZonedDateTime.withZoneSameInstant()`](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#withZoneSameInstant-java.time.ZoneId-), @user7076183. – Ole V.V. Apr 18 '17 at 20:24
2

Since some readers here will use Java 8 or later and some Java 7 or earlier, I will treat both.

I recommend you use the java.time classes introduced in Java 8 if you can:

    ZoneId targetTz = ZoneId.of("America/Los_Angeles");

    String string = "Tue Apr 18 18:58:09 +0300 2017";
    DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss ZZZ uuuu",
            Locale.ENGLISH);
    ZonedDateTime sourceTime = ZonedDateTime.parse(string, format);
    ZonedDateTime targetTime = sourceTime.withZoneSameInstant(targetTz);
    String result = targetTime.format(format);
    System.out.println(result);

This prints:

Tue Apr 18 08:58:09 -0700 2017

You said you wanted a PST date, and this is exactly what the ZonedDateTime gives you: date and time with time zone information.

In the example I am giving a zone offset, +0300 (corresponding to Israel Daylight Time) in the string. I understood that it wasn’t important to you how the time zone was given. I want to avoid the three and four letter time zone abbreviations like IST. Not only may IST mean either Irish Standard Time, Israel Standard Time or India Standard Time. I furthermore noticed that the java.time classes pick up 18:58:09 IST as 18:58:09 IDT (UTC+3) because it knows Israel is on DST on April 18; the SimpleDateFormat that I return to below takes IST more literally and interprets 18:58:09 IST as 18:58:09 +0200, which is 1 hour later in UTC.

You can use the java.time classes with Java 1.7 if you want. You can get them in the ThreeTen Backport.

If you don’t want to use java.time, the way to do it with the outdated classes from Java 1.0 and 1.1 is not that different in this case, only I cannot give you the PST date you asked for:

    TimeZone targetTz = TimeZone.getTimeZone("America/Los_Angeles");
    String string = "Tue Apr 18 18:58:09 +0300 2017";
    SimpleDateFormat dt = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);
    Date d = dt.parse(string);
    dt.setTimeZone(targetTz);
    String result = dt.format(d);
    System.out.println(result);

It prints the same result as above. However, you notice there is only one Date object. A Date object cannot hold any time zone information, so if you need this, you will have to bring the targetTz object along with d. It’s a common misunderstanding that there’s supposed to be a time zone in the Date object, probably greatly helped by the fact that its toString() prints a time zone. This is always the JVM’s default time zone and doesn’t come from the Date object, though.

Ole V.V.
  • 76,217
  • 14
  • 120
  • 142