Converting from Milliseconds to UTC Time in Java

后端 未结 3 712
粉色の甜心
粉色の甜心 2021-01-01 20:30

I\'m trying to convert a millisecond time (milliseconds since Jan 1 1970) to a time in UTC in Java. I\'ve seen a lot of other questions that utilize SimpleDateFormat to chan

3条回答
  •  自闭症患者
    2021-01-01 21:22

    java.time option

    You can use the new java.time package built into Java 8 and later.

    You can create a ZonedDateTime corresponding to that instant in time in UTC timezone:

    ZonedDateTime utc = Instant.ofEpochMilli(1427723278405L).atZone(ZoneOffset.UTC);
    System.out.println(utc);
    

    You can also use a DateTimeFormatter if you need a different format, for example:

    System.out.println( DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss").format(utc));
    

提交回复
热议问题