使用所需的时区从毫秒显示正确的时间
Showing correct time from Milliseconds with desired TimeZone
我正在开发一个从 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)
问题是:我有正确的伦敦日期和时间,但在不更改时间的情况下启用 show/change 时区,因为根据伦敦时间是正确的。
已更新
收到一些评论后。在我的例子中,你没有把我带到这里。
假设我在巴基斯坦,这里的时间是 1:55 PM
,所以我通过我的应用程序询问 GOOGLE API 告诉我伦敦现在几点。 Google API 告诉我伦敦的时间是 1504760156000
(9:55 AM) 以毫秒为单位,如果我将这些毫秒转换为 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 时区 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
时间戳表示自纪元以来经过的时间的 "absolute" 值。例如,您的 currentTimeUTCinSeconds
表示自 unix 纪元(即 1970-01-01T00:00Z
或 January 1st 1970 年午夜以来的秒数UTC). Java API 通常使用纪元以来的毫秒数。
但概念是相同的 - 这些值是 "absolute":它们对世界上的每个人都是相同的,无论他们身在何处。如果世界不同地区(不同时区)的 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新Date/TimeAPI
旧的 类(Date
、Calendar
和 SimpleDateFormat
)有 lots of problems and design issues,它们正在被新的 APIs.
在 Android 中您可以使用 ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need the ThreeTenABP (more on how to use it ).
为了从时间戳获取日期,我使用 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));
我正在开发一个从 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)
问题是:我有正确的伦敦日期和时间,但在不更改时间的情况下启用 show/change 时区,因为根据伦敦时间是正确的。
已更新
收到一些评论后。在我的例子中,你没有把我带到这里。
假设我在巴基斯坦,这里的时间是 1:55 PM
,所以我通过我的应用程序询问 GOOGLE API 告诉我伦敦现在几点。 Google API 告诉我伦敦的时间是 1504760156000
(9:55 AM) 以毫秒为单位,如果我将这些毫秒转换为 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 时区 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
时间戳表示自纪元以来经过的时间的 "absolute" 值。例如,您的 currentTimeUTCinSeconds
表示自 unix 纪元(即 1970-01-01T00:00Z
或 January 1st 1970 年午夜以来的秒数UTC). Java API 通常使用纪元以来的毫秒数。
但概念是相同的 - 这些值是 "absolute":它们对世界上的每个人都是相同的,无论他们身在何处。如果世界不同地区(不同时区)的 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新Date/TimeAPI
旧的 类(Date
、Calendar
和 SimpleDateFormat
)有 lots of problems and design issues,它们正在被新的 APIs.
在 Android 中您可以使用 ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need the ThreeTenABP (more on how to use it
为了从时间戳获取日期,我使用 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));