Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a time in milliseconds to ZonedDateTime

Tags:

java

java-time

I have the time in milliseconds and I need to convert it to a ZonedDateTime object.

I have the following code

long m = System.currentTimeMillis();
LocalDateTime d = LocalDateTime.millsToLocalDateTime(m);

The line

LocalDateTime d = LocalDateTime.millsToLocalDateTime(m);

gives me a error saying methed millsToLocalDateTime is undefined for type LocalDateTime

like image 558
Ted pottel Avatar asked Aug 21 '18 16:08

Ted pottel


People also ask

How do I get time from ZonedDateTime?

now() now() method of a ZonedDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return ZonedDateTime based on system clock with default time-zone to obtain the current date-time. The zone and offset will be set based on the time-zone in the clock.

How do I convert ZonedDateTime to another timezone?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

What is the format of ZonedDateTime?

Class ZonedDateTime. A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris . ZonedDateTime is an immutable representation of a date-time with a time-zone.


1 Answers

ZonedDateTime and LocalDateTime are different.

If you need LocalDateTime, you can do it this way:

long m = ...;
Instant instant = Instant.ofEpochMilli(m);
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
like image 103
xingbin Avatar answered Oct 16 '22 18:10

xingbin