且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java 8中获取UTC + 0日期?

更新时间:2023-02-04 20:25:09

使用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;