浩晨众云网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
一、获取当前时区时间:
Instant.now().atZone(ZoneId.systemDefault())
二、创建Instant实例
Instant now = Instant.now();
System.out.println(“now:”+now);
控制台输出:
now:2018-07-09T08:59:08.853Z
注意:通过这种方式获取的时间戳与北京时间相差8个时区,需要修正为北京时间,通过查看源代码发现Instant.now()使用等是UTC时间Clock.systemUTC().instant()。LocalDate、LocalDateTime 的now()方法使用的是系统默认时区 不存在Instant.now()的时间问题。
###解决方法
增加8个小时
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(“now:”+now);
控制台输出:
now:2018-07-09T16:58:48.188Z
三、Instant获取long类型的10位秒数、13位毫秒数
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)); System.out.println(“秒数:”+now.getEpochSecond()); System.out.println(“毫秒数:”+now.toEpochMilli());