以秒为单位的纪元时间:1529948813696000
如何将其转换为Java时间戳。
我可以使用这种方法以毫秒为单位转换时代时间
Instant instant = Instant.ofEpochMilli(Long.parseLong("1529957592000"));
Date parsedDate = dateFormat.parse(instant.atZone(ZoneId.of("America/Chicago")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")).toString())
Run Code Online (Sandbox Code Playgroud)
需要帮助将纪元时间转换为微秒吗?
虽然Instant该类没有ofEpochNano(long)方法,但确实有一个方法的重载,ofEpochSecond(long, long)即接受一个纳秒级的偏移量以及一个纪元秒。
通过一些数学运算,您可以将纪元微秒转换为秒加偏移量,然后Instant使用它来创建它,如下所示:
long epochMicroSeconds = 1_529_948_813_696_123L;
long epochSeconds = epochMicroSeconds / 1_000_000L;
long nanoOffset = ( epochMicroSeconds % 1_000_000L ) * 1_000L ;
Instant instant = Instant.ofEpochSecond( epochSeconds, nanoOffset ) ;
Run Code Online (Sandbox Code Playgroud)
参见IdeOne.com实时运行此代码。
Instant.toString():2018-06-25T17:46:53.696123Z