Java 8: Convert file time (milliseconds from 1970) to RFC 1123 format

后端 未结 3 1751
北恋
北恋 2021-01-06 00:45

This seems like it should be simple, but so far nothing I try is working. Basically I want to convert a file time in milliseconds from 1970 (the usual) to a TemporalAccessor

3条回答
  •  青春惊慌失措
    2021-01-06 01:18

    Milliseconds since 1970 is known as "epoch time", and Instant has the static method Instant.ofEpochMilli(long) to support creation from a long number of milliseconds. Conceptually Instant represents a single moment (long seconds + int nanoseconds) in "the standard Java epoch", so representing file time as an Instant is the correct JSR-310 way to do it.

    FileTime also has a fromMillis(long) static method for the same purpose.

    In this case, the conversion wasn't the problem—it was the lack of timezone as Sleafar identified first, as reflected in your edited question.

提交回复
热议问题