0

I am trying to convert time zone from UTC to GMT in java. I have tried several times and even used your guided method too. I am getting my output with correct timing in GMT format but along with "PDT 2012" written with it. Why so..?? I have tried hundreds of methods but can't get rid of it.

Please help me.

Thanks

rahull
  • 21
  • 1
  • 1
  • 2
    I doubt you have tried "hundreds of methods". In any case, note that PDT is PST *in daylight savings* and post the "best code" (with the types used) to date, along with the actual and desired results. –  Mar 15 '12 at 18:45
  • 2
    GMT or PST? Your question is unclear, and you've shown no code. In general, I strongly suggest you use Joda Time, but beyond that it's hard to help with such a vague question. Please read http://tinyurl.com/so-hints – Jon Skeet Mar 15 '12 at 18:50

2 Answers2

2

For all Date / or DateTime related operations in Java I would recommend to use JodaTime Library

It is very useful to use Date/time with different point of views (calendar, timezone) and for computation as well: adding/substracting months, years, days and so on...

Since Java 8, an equivalent (improvement) of JodaTime is included in the JDK under the new package java.time (JSR-310) and no more needed to add it as dependency. The author of JodaTime explains in his blog the difference between JodaTime and JSR-310.

рüффп
  • 5,172
  • 34
  • 67
  • 113
  • No, Joda-Time is *not* included in the JDK. The Joda-Time project *inspired* the JSR 310 project that led to the *java.time* classes built into Java 8 and letter. Both Joda-Time and JSR 310 share major concepts as they were both led by the same man, Stephen Colebourne. But *java.time* is *not* the same as Joda-Time and is *not* copy-paste code compatible. – Basil Bourque Apr 22 '18 at 04:58
  • Thank you for your comment, I edited my answer in consequence. As many people I though JodaTime was just ported into JDK but I found the Blog link which can be useful as well. – рüффп Apr 22 '18 at 09:03
1

Perhaps the following will be a starting point. It converts your current date to GMT:

    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    Calendar cal = Calendar.getInstance();
    System.out.println(cal.getTime());
    TimeZone currentTimeZone = cal.getTimeZone();
    int offset = currentTimeZone.getOffset(cal.getTimeInMillis());
    Date adjustedTime = new Date(cal.getTimeInMillis() - offset);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(dateFormat.format(adjustedTime));

A couple of notes:

  • You are probably not able see the PST change to UTC because you don't set the timezone on the date format
  • You shouldn't really use the abbreviations like "GMT" anymore. It is better to use the full name in the id field.
  • You'll have to be a bit more creative if you happen to run the above code on a system that has its default time already set to GMT.
radimpe
  • 3,197
  • 2
  • 27
  • 46
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 22 '18 at 04:59