Java 如何将 UTC 毫秒转换为 UTC 日期 [英] Java How to convert UTC milliseconds into UTC Date
问题描述
我有一个 int 1446159600,它是 UTC/GMT 日期,2015 年 10 月 29 日星期四 23:00:00 GMT.我尝试转换为实际的 UTC 日期,但无法使其与 Calendar、SimpleDateFormat 和 Timezone 类一起使用.有人可以帮我吗?
to;dr
Instant.ofEpochSecond ( 1_446_159_600L )
<块引用>
2015-10-29T23:00:00Z
从纪元开始计数
您似乎有一个整数 count-from-epoch 计算整秒在 Unix 时间 的时代,UTC.
您引用的旧日期时间类基于毫秒.所以乘以1000.
java.time
更好的是,完全避免那些旧类.新的 java.time 框架在 Java 8 及更高版本中是一个巨大的改进,取代了那些旧类.请参阅教程.这些新类的分辨率为纳秒,即几分之一秒内的 9 个小数位.
即时
类(UTC 时间线上的一个时刻)甚至有一个方便的 ofEpochSecond
方法,所以不需要乘以一千.
long wholeSecondsFromEpochInUtc = 1_446_159_600L;Instant Instant = Instant.ofEpochSecond ( wholeSecondsFromEpochInUtc );
这可以解决您的问题.
格式化
我们可以走得更远.将时区应用于该 Instant 以获得 ZonedDateTime
.
ZoneId zoneId = ZoneId.of ( "America/Montreal" );ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );DateTimeFormatter 格式化程序 = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL ).withLocale ( Locale.CANADA_FRENCH );字符串输出 = zdt.format ( formatter );
转储到控制台.
System.out.println ( "即时 (UTC): " + 即时 );System.out.println("zdt:" + zdt);System.out.println("输出:" + 输出);
<块引用>
即时 (UTC):2015-10-29T23:00:00Z
zdt: 2015-10-29T19:00-04:00[美国/蒙特利尔]
输出:jeudi 29 octobre 2015 19 h 00 EDT
I have an int 1446159600 which is UTC/GMT date Thu, 29 Oct 2015 23:00:00 GMT. I tried to do conversion to the actual UTC date, but couldn't get it to work with Calendar, SimpleDateFormat, and Timezone classes. Can someone help me?
to;dr
Instant.ofEpochSecond ( 1_446_159_600L )
2015-10-29T23:00:00Z
Count From Epoch
You appear to have an integer count-from-epoch counting whole seconds with an epoch of Unix time, the first moment of 1970 in UTC.
The old date-time classes you reference are based on milliseconds. So multiply by 1,000.
java.time
Even better, avoid those old classes altogether. The new java.time framework in Java 8 and later is a vast improvement, supplanting those old classes. See Tutorial. These new classes have a resolution of nanoseconds, or 9 decimal places in a fraction of a second.
The Instant
class (a moment on the timeline in UTC) even has a handy ofEpochSecond
method, so no need to multiply by a thousand.
long wholeSecondsFromEpochInUtc = 1_446_159_600L;
Instant instant = Instant.ofEpochSecond ( wholeSecondsFromEpochInUtc );
That solves your Question in a one-liner.
Formatting
We can go further. Apply a time zone to that Instant to get a ZonedDateTime
.
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL ).withLocale ( Locale.CANADA_FRENCH );
String output = zdt.format ( formatter );
Dump to console.
System.out.println ( "instant (UTC): " + instant );
System.out.println ( "zdt: " + zdt );
System.out.println ( "output: " + output );
instant (UTC): 2015-10-29T23:00:00Z
zdt: 2015-10-29T19:00-04:00[America/Montreal]
output: jeudi 29 octobre 2015 19 h 00 EDT
这篇关于Java 如何将 UTC 毫秒转换为 UTC 日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!