0

I am trying to convert a date(Date object) with one time zone (say IST) to a date in GMT. But i am getting a date which does have a changed time but the timezone it displays is still "IST". i want the timezone should be displayed as "GMT".

  1. I used a Date object to get the final date after trying several conversions, which gave the converted date but the time zone was still "IST" in the output.

  2. I then tried with DateTime object(Joda Time) :

    Date dateIndia = new Date();
    DateTime  dateTime = new DateTime(dateIndia);
    
    DateTimeZone timeZone = DateTimeZone.forID("GMT");
    DateTime date_UtcGmt = dateTime.toDateTime(timeZone);
    
    System.out.println("dateIndia : " + dateIndia);
    System.out.println("dateTime : " + dateTime);
    System.out.println("date_UtcGmt : " + date_UtcGmt);
    System.out.println("date_UtcGmt.toDate : " + date_UtcGmt.toDate());
    

It is showing me the following output :

    dateIndia : Thu Sep 04 15:49:50 IST 2014
    dateTime : 2014-09-04T15:49:50.152+05:30
    date_UtcGmt : 2014-09-04T10:19:50.152Z
    date_UtcGmt.toDate : Thu Sep 04 15:49:50 IST 2014

Now i have date_UtcGmt in GMT format but when i convert it to date, it is again in IST format. I want a date object which shows GMT as its time zone. Can i change the display format of date_UtcGmt object? What should i do?

sonal
  • 101
  • 1
  • 4
  • 13
  • Have you looked at the `withZone` method? http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#withZone(org.joda.time.DateTimeZone) – reto Sep 04 '14 at 10:54
  • `java.util.Date` objects don't have a concept of a timezone, so you can't *convert* one from one zone to another. But you can *print* a `Date` in a particular timezone - is that what you want? – Duncan Jones Sep 04 '14 at 10:57
  • Hi reto, i need a "Date" object in GMT format. withZone is giving the same output as given by : System.out.println("date_UtcGmt : " + date_UtcGmt); – sonal Sep 04 '14 at 10:59
  • Hi Duncan, yes after converting the date i want "GMT" to be printed instead of "IST" here : "Thu Sep 04 15:49:50 IST 2014". But i want a date object as i need a GMT time in date object to use further. – sonal Sep 04 '14 at 11:01
  • So something like this question should help (or be a duplicate?): [How do I display a date with a custom timezone?](http://stackoverflow.com/questions/1166494/how-do-i-display-a-date-with-a-custom-timezone) – Duncan Jones Sep 04 '14 at 11:03
  • Hi Duncan, This is printing the date in a different timezone. But i really want a 'Date' object with the new time zone so that when i print the new date, it prints the new timezone that we have set. – sonal Sep 04 '14 at 11:09
  • possible duplicate of [How to set time zone of a java.util.Date?](http://stackoverflow.com/questions/2891361/how-to-set-time-zone-of-a-java-util-date) – Basil Bourque Sep 08 '14 at 16:09

3 Answers3

2

You can't solve this problem using java.util.Date. That class doesn't store a timezone, so you have to do timezone manipulations at the moment you print the date.

With Joda time, you can achieve the desired result with DateTime and withZone():

DateTime dt = new DateTime();
System.out.println(dt);

DateTime gmt = dt.withZone(DateTimeZone.forID("GMT"));
System.out.println(gmt);

For me (in Britain), this prints:

2014-09-04T12:16:33.357+01:00
2014-09-04T11:16:33.357Z
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
1

Shiver mi timbers...

a) Date does not have a timezone, read the javadoc.

b) The toString() method of date prints out stuff in the timezone of the JVM and for the love of god don't change that for date transformation.

c) You set timezone GMT and call the variable UtcGmt. GMT and UTC are not entirely the same, furthermore they are both timezones and you specifically set it to GMT, so why is UTC in the name?

d) You want to "show it" in a certain timezone? Where? When you are printing it to console? In a web application? Desktop application? Some of the frameworks have custom formatters but if you want to format it yourself, use SimpleDateFormat (or whatever Joda offers). A date in and of itself is merely a point in time expressed as a long without timezone, language, format,...

nablex
  • 4,635
  • 4
  • 36
  • 51
-1

We can set the default time zone to do this. Try like following:

TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
System.out.println("date_UtcGmt.toDate : " + date_UtcGmt.toDate());

Output will be like,

date_UtcGmt.toDate : Thu Sep 04 11:44:20 GMT 2014
yuva 443
  • 163
  • 1
  • 2
  • 11