6

I have done this for my Calendar instance to return Date in UTC timezone:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:SS Z");
TimeZone tz = TimeZoneUtil.getTimeZone(StringPool.UTC);
formatter.setTimeZone(tz);

    Date dtStart = null;
    Date dtEnd = null;

    try{
        dtStart = formatter.parse(formatter.format(startDate.getTime()));
        dtEnd = formatter.parse(formatter.format(endDate.getTime()));
    }catch (Exception e) {
        e.getStackTrace();
}

It works fine till I format calendar timestamp to return a string date with required timezone but when I parse that string date to Date date, it again picks up local timezone? I need to store Date object in UTC timezone.

Any help will be highly appreciated!

Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
  • possible duplicate of [How can I get the current date and time in UTC or GMT in Java?](http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java) – Peter Mortensen Dec 04 '14 at 04:14

4 Answers4

8

You can use this:

 Date localTime = new Date(); 

 //creating DateFormat for converting time from local timezone to GMT
 DateFormat converter = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss");

 //getting GMT timezone, you can get any timezone e.g. UTC
 converter.setTimeZone(TimeZone.getTimeZone("GMT"));

 System.out.println("local time : " + localTime);;
 System.out.println("time in GMT : " + converter.format(localTime));

It will give:
local time: Fri Jun 21 11:55:00 UTC 2013
time in GMT : 21/06/2013:11:55:00

I hope it will help.

Cheers.

Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
Vahe
  • 96
  • 3
0

Date object in java will always store the values in the host machine (your system) time zone information.

This is from javadoc :

Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine.

You should trying using Joda Time which is much advanced.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • This is not true, the default Date() constructor stores the value of [System.currentTimeMillis()](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis%28%29), which is the number of milliseconds since January 1, 1970 UTC. As you mentioned there are slight discrepancies between "computer time" and UTC, see the discussion on [leap seconds](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html). But Joda Time does not offer any advantages in this respect, see [Are leap seconds supported](http://joda-time.sourceforge.net/faq.html#leapseconds). – devconsole Jun 21 '13 at 15:04
0

Instead of setting TimeZone in multiple places, it is a good idea to set timezone using -Duser.timezone=GMT or PST.

And, you can easily test how Java deals with timezone and getTime() ignores timezone with an actual example:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); // print with timezone


        TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of("GMT"));
        TimeZone.setDefault(timeZone);          // set system timezone as GMT
        sdf.setTimeZone(timeZone);              // formatter also has a timezone
        Date date = new Date();
        System.out.println(date);               // system says GMT date
        System.out.println(date.getTime());     // only prints time in milliseconds after January 1, 1970 00:00:00 GMT
        System.out.println(sdf.format(date));

        timeZone = TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles"));
        TimeZone.setDefault(timeZone);          // set system timezone as GMT
        sdf.setTimeZone(timeZone);              // formatter also has a timezone
        date = new Date();
        System.out.println(date);
        System.out.println(date.getTime());     // prints the same value as above, "not including timezone offset"
        System.out.println(sdf.format(date));

        // GMT and PDT times are same as getTime() only returns time in ms since UTC for the day ignoring timezone which is mostly used for formatting

Wed Mar 14 22:43:43 GMT 2018
1521067423108
2018-03-14T22:43:43+0000

Wed Mar 14 15:43:43 PDT 2018
1521067423125                  // not includes timezone in getTime()
2018-03-14T15:43:43-0700       // formatting looks fine
kisna
  • 2,869
  • 1
  • 25
  • 30
-1

The good explanation of why Date object taking Current time zone value ,

please refer this SO answer

EDIT.


here I am gonna add some important part of that answers.


java.util.Date is has no specific time zone, although its value is most commonly thought of in relation to UTC. What makes you think it's in local time?

To be precise: the value within a java.util.Date is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. The same epoch could also be described in other time zones, but the traditional description is in terms of UTC. As it's a number of milliseconds since a fixed epoch, the value within java.util.Date is the same around the world at any particular instant, regardless of local time zone.

Community
  • 1
  • 1
dharmendra
  • 7,835
  • 5
  • 38
  • 71