问题
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.
回答1:
Two messages:
- Your expectations are wrong. A
Datehasn’t got a time zone, it cannot have. So what you are trying to obtain is impossible usingDateandSimpleDateFormatno matter how you write the code. - The classes
Date,SimpleDateFormatandTimeZoneare 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.bpand subpackages.
Links
- Oracle tutorial: Date Time, explaining how to use
java.time. - ThreeTen Backport project
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Java Specification Request (JSR) 310.
回答2:
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"));
来源:https://stackoverflow.com/questions/52475409/getting-the-current-time-millis-from-device-and-converting-it-into-a-new-date-wi