Parkash Kumar
Parkash Kumar

Reputation: 4730

Calendar.getTime() not returning UTC date if TimeZone is defined

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!

Upvotes: 6

Views: 14934

Answers (4)

kisna
kisna

Reputation: 3137

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

Upvotes: 0

Vahe
Vahe

Reputation: 96

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.

Upvotes: 8

dharmendra
dharmendra

Reputation: 7881

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.

Upvotes: -1

Abubakkar
Abubakkar

Reputation: 15664

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.

Upvotes: 0

Related Questions