如何在Java 8中获取UTC + 0日期? [英] How to get UTC+0 date in Java 8?
本文介绍了如何在Java 8中获取UTC + 0日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经google了,找到了很好的JavaScript解决方案,但Java没有什么用。
如何在Java 8中获取UTC + 0日期?
解决方案
使用Java 8可以写:
ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
要回答您的评论,您可以将其转换为日期(除非您依赖于旧版代码没有看到任何理由)或从以后的时间开始:
Date date = Date.from(utc.toInstant ));
long epochMillis = utc.toEpochSecond()* 1000;
I have problems with Date class in Java. Date class returns local machine date but i need UTC-0.
I have googled and found great solution for JavaScript but for Java nothing useful.
How to get UTC+0 date in Java 8?
解决方案
With Java 8 you can write:
ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
To answer your comment, you can then convert it to a Date (unless you depend on legacy code I don't see any reason why) or to millis since the epochs:
Date date = Date.from(utc.toInstant());
long epochMillis = utc.toEpochSecond() * 1000;
这篇关于如何在Java 8中获取UTC + 0日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文