从设备获取当前时间毫秒,并将其转换为具有不同时区的新日期

问题描述:

这里我面临的问题是-

首先,我创建了一个date对象,它将为我提供设备时区的当前日期和时间,即

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"

不,我有一个Date格式化程序,使用它我转换了以下日期对象。

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.

我已经尝试过 Z, z, X, ZZ,日期格式器中的ZZZZZ仍然没有运气。

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.

两条消息:


  • 您的期望是错误的。 日期没有时区,所以没有。因此,无论您如何编写代码,都无法使用 Date SimpleDateFormat 来获取。

  • Date SimpleDateFormat TimeZone 早已过时且设计欠佳。它们的现代替代品是在2014年引入的java.time(日期和时间API)中。

  • 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);

此代码段的输出为:


2018/09/24 18:43:19 HST

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

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



问题:我可以在Android上使用 java.time 吗?



是的, java.time 在较新的Android设备上都能很好地工作。它只需要至少 Java 6

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.


  • 在Java 8和更高版本以及新的Android设备上(通过API)有人告诉我,第26级是内置的现代API。

  • 在Java 6和7中,获得了ThreeTen Backport,这是现代类的Backport(JSR 310的ThreeTen,其中

  • 在(较旧的)Android上,使用Android版的ThreeTen Backport。叫做ThreeTenABP。确保从包 org.threeten.bp 和子包中导入日期和时间类。

  • 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.
  • 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.