How can I convert a time in milliseconds to ZonedDateTime

前端 未结 4 610
忘了有多久
忘了有多久 2021-01-13 03:18

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();
L         


        
4条回答
  •  我在风中等你
    2021-01-13 03:46

    You cannot create extension methods in Java. If you want to define a separate method for this create a Utility class:

    class DateUtils{
    
        public static ZonedDateTime millsToLocalDateTime(long m){
            ZoneId zoneId = ZoneId.systemDefault();
            Instant instant = Instant.ofEpochSecond(m);
            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
            return zonedDateTime;
        }
    }
    

    From your other class call

    DateUtils.millsToLocalDateTime(89897987989L);
    

提交回复
热议问题