Java: Long Timestamp + int offset 转换为可读格式

Java: transform Long Timestamp + int offset to readable format

我正在尝试将带有偏移量 (int) 的 Long 时间戳转换为更易读的格式。因此,例如,我将 1514564669355 作为偏移量等于 360 的时间戳。我应该如何使用 java 将其转换为等效的日期格式?

在这种情况下,时间戳将存储在 UTC 中,因此我希望使用偏移量将其转换为所需的任何时区。感谢任何 help/tips。

你的问题不清楚。

  • 也许您的意思是您有一个 UTC 值,并希望将其调整为比 UTC 早 360 分钟的偏移量。 (一种糟糕的沟通方式。)
  • 或者您的意思是给定值已经早于 UTC。 (一种更糟糕的沟通方式。)

以 UTC 开始

首先解析您输入的号码。我们将假设该数字是自 1970 年 UTC 时间 1970-01-01T00:00Z 以来的毫秒数。

Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds(最多九 (9) 位小数)。

Instant instant = Instant.ofEpochMilli( 1_514_564_669_355L ) ;

instant.toString(): 2017-12-29T16:24:29.355Z

一个offset-from-UTC is a number of hours, minutes, and seconds ahead of, or behind, UTC.

也许你的问题提到的偏移量 360 是指 360 分钟,或者比 UTC 早 6 小时。

ZoneOffsetclass无法从超过59的分钟数实例化,所以我们从360分钟转换为总秒数

int seconds = ( int ) TimeUnit.MINUTES.toSeconds( 360L ) ;  // Convert minutes to seconds.
ZoneOffset offset = ZoneOffset.ofTotalSeconds( seconds );   // Determine offset-from-UTC.

offset.toString(): +06:00

将 UTC 中的 Instant 调整到此偏移量,产生一个 OffsetDateTime 对象。同一时刻,时间轴上的同一点,不同的挂钟时间。因此,一天中的时间被视为晚上 10 点而不是下午 4 点,因为 ( 16 + 6 ) = 22.

    OffsetDateTime odt = instant.atOffset( offset );

odt.toString(): 2017-12-29T22:24:29.355+06:00

从偏移量开始

也许您的意思是所传达的时刻是 UTC 的毫秒数,但是 360 分钟中的毫秒数已经被添加或减去。

顺便说一下,这是一种非常糟糕的交换日期时间值的方法。就 ISO 8601 标准对您的数据供应商进行培训。

让我们撤消偏移量的 addition/subtraction,返回到 UTC。

long millisInOffset = TimeUnit.MINUTES.toMillis( 360L );  // Convert minutes to milliseconds.
long millisSinceEpoch = ( 1_514_564_669_355L - millisInOffset );
Instant instant = Instant.ofEpochMilli( millisSinceEpoch );

instant.toString(): 2017-12-29T10:24:29.355Z

在发送给我们的偏移量中查看该值。

int seconds = ( int ) TimeUnit.MILLISECONDS.toSeconds( millisInOffset );
ZoneOffset offset = ZoneOffset.ofTotalSeconds( seconds );
OffsetDateTime odt = instant.atOffset( offset );
System.out.println( odt );

2017-12-29T16:24:29.355+06:00


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* classes.

从哪里获得java.time classes?

  • Java SE 8, Java SE 9, Java SE 10,及以后
    • 内置。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and Java SE 7
  • Android
    • Android java.time classes.
    • 捆绑实施的更高版本
    • 对于较早的 Android (<26),ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.