static final long NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1L);
static final long NANOS_PER_MICROSECOND = TimeUnit.MICROSECONDS.toNanos(1L);
/**
* Obtains an instance of Instant using nanoseconds from the epoch of 1970-01-01T00:00:00Z.
*
* @param epochNano the number of nanoseconds from 1970-01-01T00:00:00Z.
* @return an instant; not {@code null}.
*/
public static Instant instantOfEpochNano(final long epochNano) {
return Instant.ofEpochSecond(0L, epochNano);
}
/**
* Obtains an instance of Instant using microseconds from the epoch of 1970-01-01T00:00:00Z.
*
* @param epochMicro the number of microseconds from 1970-01-01T00:00:00Z.
* @return an instant; not {@code null}.
* @see #instantOfEpochNano(long)
*/
public static Instant instantOfEpochMicro(final long epochMicro) {
return instantOfEpochNano(Math.multiplyExact(epochMicro, NANOS_PER_MICROSECOND));
}
2条答案
按热度按时间hts6caw31#
谢谢你的建议。这是我能想到的最干净的:
测试用例:
yftpprvb2#
这是accepted answer的简化方法。