本文实例讲述了Java实现纪元秒和本地日期时间互换的方法。分享给大家供大家参考,具体如下:
Java版本:1.8开始
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; /** * Created by Frank */ public class CurrentDatetime { public static void main(String[] args) { // 纪元秒和本地日期时间互换 Instant epochSec = Instant.ofEpochSecond(1000000000L); ZoneId zId = ZoneId.systemDefault(); ZonedDateTime then = ZonedDateTime.ofInstant(epochSec, zId); System.out.println( "The epoch was a billion seconds old on " + then); /* 运行输出: The epoch was a billion seconds old on 2001-09-09T09:46:40+08:00[Asia/Shanghai] */ } } |
eclipse运行结果如下:
希望本文所述对大家java程序设计有所帮助。