我正在开发一个从 Google TimeZone API
获取数据的应用程序。
简单地说,我有时间以毫秒为单位到达地球上所需的位置。
例如:1504760156000
它显示伦敦的日期时间
Thu Sep 07 2017 09:55:56
因为我在 UTC 的时区 +05:00
,如果我操作 1504760156000
这些毫秒,它将显示整个日期时间,如下所示:
Thu Sep 07 2017 09:55:56 GMT+0500 (Pakistan Standard Time)
但我想展示:
Thu Sep 07 2017 09:55:56 GMT+0100 (British Summer Time)
问题是:我有正确的伦敦日期和时间,但能够在不更改时间的情况下显示/更改时区,因为根据伦敦时间是正确的。
已更新
在收到一些评论后。在我的例子中,你没有把我带到这里。
假设我在巴基斯坦,这里的时间是 1:55 PM
,所以我通过我的应用程序请求 GOOGLE API 告诉我伦敦现在几点。 Google API 告诉我伦敦的时间是 1504760156000
(上午 9:55),如果我将这些毫秒转换为 Date
对象,它将打印如下:
Date date =new Date(1504760156000)
Thu Sep 07 2017 09:55:56 GMT+0500 (Pakistan Standard Time)
它将根据我的本地时区对其进行操作,但我想要如下所示的结果
Thu Sep 07 2017 09:55:56 GMT+0100 (British Summer Time)
更新 2
我准备了以秒为单位的 UTC 时间戳,因为 Google Time Zone API 需要以秒为单位的时间戳 UTC
"https://maps.googleapis.com/maps/api/timezone/json?location="+Latitude +","+Longitude+"×tamp="+currentTimeUTCinSeonds+"&key="API KEY"
Google API 使用以下针对伦敦的 JSON 响应我。
{
"dstOffset" : 3600,
"rawOffset" : 0,
"status" : "OK",
"timeZoneId" : "Europe/London",
"timeZoneName" : "British Summer Time"
}
根据文档:
Calculating the Local Time
The local time of a given location is the sum of the timestamp parameter, and the dstOffset and rawOffset fields from the result.
我总结了结果timestamp+rawoffset+dstoffset*1000='1504760156000'
(在我尝试的时候)
项目代码
Long ultimateTime=((Long.parseLong(timeObject1.getDstOffset())*1000)+(Long.parseLong(timeObject1.getRawOffset())*1000)+timestamp*1000);
timeObject1.setTimestamp(ultimateTime); //its Sime POJO object to save current time of queried Location
Date date=new Date(ultimateTime);
date1.setText("Date Time : "+date);
正如我所说,我在本地时区操作结果,所以它当时给了我以下结果:
Thu Sep 07 2017 09:55:56 GMT+0500 (Pakistan Standard Time)
但我知道 API 给了我正确的时间。问题是与 UTC 的本地偏移量。我只想将 GMT+0500
更改为 GMT+0100
最佳答案
时间戳表示自纪元以来耗时的“绝对”值。例如,您的 currentTimeUTCinSeconds
表示自 unix 纪元(即 1970-01-01T00:00Z
,或 1 月 1 日st 1970 年午夜 (UTC))。 Java API 通常使用自纪元以来的毫秒数。
但概念是一样的——这些值(value)观是“绝对的”:它们对世界上的每个人都是一样的,无论他们身在何处。如果世界不同地区(不同时区)的 2 个人同时获得当前时间戳,他们将获得相同的数字。
不同的是,在不同的时区,这个相同的数字代表不同的本地日期和时间。
例如,您使用的时间戳对应于 Sep 7th 2017 08:55:56 UTC,其值为 1504774556(自纪元以来的秒数)。同样的数字对应于伦敦的 09:55、卡拉奇的 13:55、东京的 17:55 等等。 更改此数字会更改每个人的本地时间 - 无需操纵它。
如果你想得到一个代表这个时刻的java.util.Date
,只需这样做:
int currentTimeUTCinSeconds = 1504774556;
// cast to long to not lose precision
Date date = new Date((long) currentTimeUTCinSeconds * 1000);
此日期将保留值 1504774556000(自纪元以来的毫秒数)。此值对应于伦敦的 09:55、卡拉奇的 13:55 和东京的 17:55。
但打印此日期会将其转换为您的 JVM 默认时区(here 是对 Date::toString()
方法行为的一个很好的解释)。当您执行 "Date Time : "+date
时,它会隐式调用 toString()
方法,结果是将日期转换为您的默认时区。
如果您想要特定格式和特定时区的日期,您将需要 SimpleDateFormat
。仅打印日期(使用 System.out.println
或通过记录它)是行不通的:您不能更改日期对象本身的格式,因为 Date
has no format .
我还使用 java.util.Locale
来指定月份和星期几必须使用英语。如果您不指定区域设置,它将使用系统默认设置,并且不能保证始终是英语(这可以更改,即使在运行时也是如此,因此最好始终指定区域设置):
// use the same format, use English for month and day of week
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzzz)", Locale.ENGLISH);
// set the timezone I want
sdf.setTimeZone(TimeZone.getTimeZone("Europe/London"));
// format the date
System.out.println(sdf.format(date));
输出将是:
Thu Sep 07 2017 09:55:56 GMT+0100 (British Summer Time)
请注意,我不需要操纵时间戳值。我不使用 google API,但我认为他们的解释太困惑了,上面的代码以更少的复杂性实现了相同的结果。
在您的具体情况下,您可以:
date1.setText("Date Time : "+sdf.format(date));
Java 新日期/时间 API
旧类(Date
、Calendar
和SimpleDateFormat
)有lots of problems和 design issues ,并且它们正在被新的 API 取代。
在 Android 中,您可以使用 ThreeTen Backport ,Java 8 的新日期/时间类的一个很好的反向移植。要使其正常工作,您还需要 ThreeTenABP (更多关于如何使用它的信息 here)。
要从时间戳获取日期,我使用 org.threeten.bp.Instant
和 org.threeten.bp.ZoneId
将其转换为时区, 创建一个 org.threeten.bp.ZonedDateTime
。然后我使用 org.threeten.bp.format.DateTimeFormatter
对其进行格式化:
int currentTimeUTCinSeconds = 1504774556;
// get the date in London from the timestamp
ZonedDateTime z = Instant.ofEpochSecond(currentTimeUTCinSeconds).atZone(ZoneId.of("Europe/London"));
// format it
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'XX (zzzz)", Locale.ENGLISH);
System.out.println(fmt.format(z));
输出是一样的:
Thu Sep 07 2017 09:55:56 GMT+0100 (British Summer Time)
在您的情况下,只需执行以下操作:
date1.setText("Date Time : "+fmt.format(z));
关于android - 使用所需的时区从毫秒显示正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46093200/