将 Instant 从大纪元时间转换为微秒
Convert Instant to microseconds from Epoch time
在Instant
中有方法:
toEpochMilli
将此瞬间转换为从 1970-01-01T00:00:00Z 纪元算起的毫秒数
getEpochSecond
其中 从 1970-01-01T00:00:00Z. 的 Java 纪元获取秒数
这两种方法都失去了精度,例如在 toEpochMilli
Java文档中我看到:
If this instant has greater than millisecond precision, then the conversion
drop any excess precision information as though the amount in nanoseconds
was subject to integer division by one million.
我没有看到相应的方法来获取更精确的时间戳。 如何从 Java 8 的纪元中获取微米或纳米的数量?
将 getNano()
与 getEpochSeconds()
一起使用。
int getNano()
Gets the number of nanoseconds, later along the time-line, from the start of the second.
The nanosecond-of-second value measures the total number of nanoseconds from the second returned by getEpochSecond
.
使用 TimeUnit
转换为所需的单位,如评论所建议:
Instant inst = Instant.now();
// Nano seconds since epoch, may overflow
long nanos = TimeUnit.SECONDS.toNanos(inst.getEpochSecond()) + inst.getNano();
// Microseconds since epoch, may overflow
long micros = TimeUnit.SECONDS.toMicros(inst.getEpochSecond()) + TimeUnit.NANOSECONDS.toMicros(inst.getNano());
你也可以看看他们什么时候溢出:
// 2262-04-11T23:47:16.854775807Z
Instant.ofEpochSecond(TimeUnit.NANOSECONDS.toSeconds(Long.MAX_VALUE),
Long.MAX_VALUE % TimeUnit.SECONDS.toNanos(1));
// +294247-01-10T04:00:54.775807Z
Instant.ofEpochSecond(TimeUnit.MICROSECONDS.toSeconds(Long.MAX_VALUE),
TimeUnit.MICROSECONDS.toNanos(Long.MAX_VALUE % TimeUnit.SECONDS.toMicros(1)))
作为 java.time 的一部分,java.time.temporal.ChronoUnit 下有一些单位,可用于获取两个时间点之间的差异,作为几乎任何单位的数字。
例如
import java.time.Instant;
import java.time.temporal.ChronoUnit;
ChronoUnit.MICROS.between(Instant.EPOCH, Instant.now())
给出该 Instant 自纪元以来的微秒数。
尝试Google番石榴:Instants.toEpochMicros(即时)
在Instant
中有方法:
toEpochMilli
将此瞬间转换为从 1970-01-01T00:00:00Z 纪元算起的毫秒数
getEpochSecond
其中 从 1970-01-01T00:00:00Z. 的 Java 纪元获取秒数
这两种方法都失去了精度,例如在 toEpochMilli
Java文档中我看到:
If this instant has greater than millisecond precision, then the conversion drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.
我没有看到相应的方法来获取更精确的时间戳。 如何从 Java 8 的纪元中获取微米或纳米的数量?
将 getNano()
与 getEpochSeconds()
一起使用。
int getNano()
Gets the number of nanoseconds, later along the time-line, from the start of the second. The nanosecond-of-second value measures the total number of nanoseconds from the second returned by
getEpochSecond
.
使用 TimeUnit
转换为所需的单位,如评论所建议:
Instant inst = Instant.now();
// Nano seconds since epoch, may overflow
long nanos = TimeUnit.SECONDS.toNanos(inst.getEpochSecond()) + inst.getNano();
// Microseconds since epoch, may overflow
long micros = TimeUnit.SECONDS.toMicros(inst.getEpochSecond()) + TimeUnit.NANOSECONDS.toMicros(inst.getNano());
你也可以看看他们什么时候溢出:
// 2262-04-11T23:47:16.854775807Z
Instant.ofEpochSecond(TimeUnit.NANOSECONDS.toSeconds(Long.MAX_VALUE),
Long.MAX_VALUE % TimeUnit.SECONDS.toNanos(1));
// +294247-01-10T04:00:54.775807Z
Instant.ofEpochSecond(TimeUnit.MICROSECONDS.toSeconds(Long.MAX_VALUE),
TimeUnit.MICROSECONDS.toNanos(Long.MAX_VALUE % TimeUnit.SECONDS.toMicros(1)))
作为 java.time 的一部分,java.time.temporal.ChronoUnit 下有一些单位,可用于获取两个时间点之间的差异,作为几乎任何单位的数字。 例如
import java.time.Instant;
import java.time.temporal.ChronoUnit;
ChronoUnit.MICROS.between(Instant.EPOCH, Instant.now())
给出该 Instant 自纪元以来的微秒数。
尝试Google番石榴:Instants.toEpochMicros(即时)