Dhinakar
Dhinakar

Reputation: 4151

Timezone changed Automatically when create new Date object in Java

I've timestamp based on IST ( Indian Standard Time / GMT + 5:30 Hrs), My application shows last activity time based on device timestamp. that is current time - activity timestamp. Its fine when the device timezone is IST.

For example,

Activity timestamp - 11/Feb/2016 09:00:00 AM
Current timestamp (IST)  - 11/Feb/2016 10:00:00 AM
My Calculation = Current timestamp - Activity timestamp
So Application shows 1 hr ago

But the device timezone changed to some other like PHT for the same time (Philippine Time / GMT + 8 Hr)

Activity timestamp - 11/Feb/2016 09:00:00 AM
Current timestamp(PHT)  - 11/Feb/2016 12:30:00 AM (plus 2:30Hrs compare with IST)
My Calculation = Current timestamp - Activity timestamp
So Application shows 3 hrs 30 mis ago

My problem is, How to get the IST time always using java? whatever the timezone, I need IST time.

I tried below code, When i change the value to IST but the timezone automatically changed to device timezone.

Please refer below URL for source code http://goo.gl/dnvQF5

 SimpleDateFormat sd = new SimpleDateFormat(
        "yyyy.MM.dd G 'at' HH:mm:ss z");
    Date date = new Date();
    // TODO: Avoid using the abbreviations when fetching time zones.
    // Use the full Olson zone ID instead.
    sd.setTimeZone(TimeZone.getTimeZone("GMT"));
    String gmtDate = sd.format(date);
    System.out.println("GMT --> " + gmtDate);
    String istDate = gmtDate.replace("GMT", "IST");
    System.out.println("After Replace ->  " + istDate);
    sd.setTimeZone(TimeZone.getTimeZone("IST"));
    try {
        Date istConvertedDate = sd.parse(gmtDate);
        System.out.println("After Convert --> " + istConvertedDate);
    } catch (ParseException ex) {
        ex.printStackTrace();
    }

I got output like

GMT --> 2016.02.11 AD at 05:20:07 GMT                                                                                                                                              
After Replace ->  2016.02.11 AD at 05:20:07 IST                                                                                                                                    
After Convert --> Thu Feb 11 00:20:07 EST 2016 

Please help me to resolve this.

Upvotes: 0

Views: 2573

Answers (4)

Meno Hochschild
Meno Hochschild

Reputation: 44061

The class java.util.Date is only a thin wrapper around elapsed milliseconds since UNIX epoch (1970-01-01T00:00:00Z). Objects of this type don't carry any format or timezone information. Therefore every such object has completely lost the timezone information after parsing a text with timezone identifier or name using SimpleDateFormat.

What you observe and are confused about is the fact that the method toString() of this class uses a specific representation based on the system timezone.

Another thing: If you apply a string manipulation replacing "GMT" by "IST" (an ambivalent timezone name - Israel? India? Ireland?) then you effectively change the moment/instant while keeping the local time representation. Do you really want this?

If you want to preserve the timezone information which was originally parsed then you could use ZonedDateTime in the library Threeten-ABP or DateTime in the library Joda-Time-Android or ZonalDateTime in my library Time4A.

Upvotes: 2

Khan Abdulrehman
Khan Abdulrehman

Reputation: 854

Replace

sd.setTimeZone(TimeZone.getTimeZone("IST"));

to

TimeZone.setDefault(TimeZone.getTimeZone("IST"));

Upvotes: 0

achist
achist

Reputation: 51

You can use java.util.Calendar

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("IST"));

this calendar contains current time in IST timeZone.

You can set timeStamp:

calendar.setTimeInMillis(timeStamp);

And get java.util.Date or time stamp

Date date = calendar.getTime();
Long timeStamp = calendar.getTimeInMillis();

Upvotes: 0

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

Try

 public static void main(String[] args) {
    SimpleDateFormat sd = new SimpleDateFormat(
        "yyyy.MM.dd G 'at' HH:mm:ss z");
    Date date = new Date();
    // TODO: Avoid using the abbreviations when fetching time zones.
    // Use the full Olson zone ID instead.
    sd.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(sd.format(date));
    String gmtDate = sd.format(date);
    System.out.println(gmtDate);

    //Here you create a new date object
    Date istDate= new Date();
    sd.setTimeZone(TimeZone.getTimeZone("IST"));
    String istDate=sd.format(istDate);
    System.out.println(istDate)

   }

This way the first printed time will be GMT and second will be ISD.

Upvotes: 1

Related Questions