1

Here the that problem that i'm facing is -

First i have created a date object which will give me current date and time with device timezone i.e

Date date = new Date(); // Let say the time zone is India - GMT (+05:30)
The value of date is = "Mon Sep 24 13:54:06 GMT+05:30 2018"

No i have a Date formatter using which i have converted the following date object.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone(loadPreferences(Utility.TIMEZONE_NAME)));
// Here the timezone is Hawaii (GMT-10:00)

Now getting the time as per the new time zone i.e., Hawaii

String dateS = sdf.format(date); 
// This will give you the date with new timezone - "2018/09/23 22:24:06 GMT-10:00"

Now converting this string date to date object as -

Date newDate = sdf.parse(dateS);

Now the new date which i'm getting is not as per the timezone which i have passed.

The value of newDate which i'm getting is = "Mon Sep 24 13:54:06 GMT+05:30 2018" 
//This is device timezone not the one i have set.

I have already tried "Z", "z", "X", "ZZ", "ZZZZZ" in the date formatter still no luck.

If any of you have any idea reading this then let me know.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Savin Sharma
  • 769
  • 5
  • 22
  • @amit need solution for android not for .net – Savin Sharma Sep 24 '18 at 09:24
  • If you just want to transport/pass your `Date`-object around across different time zones then just pass the object. No need to take into account the zones or to format and to reparse. A `Date`-object is just a thin wrapper around the count of milliseconds since 1970-01-01T00Z (worldwide the same!). – Meno Hochschild Sep 24 '18 at 09:44
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. And includes a class for a date and time *with* time zone, the `ZonedDateTime` class, which seems to be what you need. – Ole V.V. Sep 24 '18 at 15:29
  • @OleV.V. yes.. you are right... – amit Sep 24 '18 at 15:34

2 Answers2

1

Two messages:

  • Your expectations are wrong. A Date hasn’t got a time zone, it cannot have. So what you are trying to obtain is impossible using Date and SimpleDateFormat no matter how you write the code.
  • The classes Date, SimpleDateFormat and TimeZone are long outdated and poorly designed. Their modern replacements are in java.time, the date and time API introduced in 2014.

ZonedDateTime

A modern ZonedDateTime has a time zone as the name says:

    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss z", Locale.US);
    ZonedDateTime nowInHawaii = ZonedDateTime.now(ZoneId.of("Pacific/Honolulu"));
    String dateS = nowInHawaii.format(formatter);
    System.out.println(dateS);

Output from this snippet was:

2018/09/24 18:43:19 HST

If you want the offset in the output, change the formatter thusly:

    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss OOOO", Locale.US);

2018/09/24 18:45:53 GMT-10:00

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Please try to debug the value that is coming from 'loadPreferences(Utility.TIMEZONE_NAME)' and make sure it is same as "US/Hawaii"

Also try to debug by using this -

sdf.setTimeZone(TimeZone.getTimeZone("US/Hawaii"));
Anirban Roy
  • 180
  • 6