Mark Mark - 5 months ago 19
Java Question

How to get UTC+0 date in java8?

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 usefull.

How to get UTC+0 date in Java 8?

Answer

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;