Java 8: convert file time (milliseconds since 1970) to RFC 1123 format

It seems to be simple, but so far I'm not trying to work. Basically I want to convert the file time in milliseconds from 1970 (usually) to TemporalAccessor, and then to RFC 1123 string . However, although I can get compilation examples, I get runtime errors. For instance:

// Just using 0 milliseconds time for quick and easy test
System.out.println(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME.format(
                FileTime.from(0, TimeUnit.MILLISECONDS).toInstant()));

leads to

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth

I tried several variations of this using different classes (Instant, LocalTime, Date), but I get the same result.

What is the right way to do this?

UPDATE: The initial question was technically given, and I realized that I needed to be more specific. I have already “successfully” converted milliseconds to an instance of TemporalAccessor, however, it seems that this object was not in a healthy state. I had a runtime error while trying to use it to accomplish what I really needed, which made me believe that I did not create it properly. Something is missing. Either this, or there is an error in the RFC 1123 format.

UPDATE 2: Thanks Sleafar for posting a working answer.

Using his example, I did it a little differently, because for some reason I really wanted the "full" TemporalAccessor to do something. Here is a working example:

TemporalAccessor time = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0),
ZoneId.systemDefault());
System.out.println(
        java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME.format(time));
+4
source share
3 answers

Instant . :

System.out.println(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME
    .withZone(ZoneId.systemDefault()).format( FileTime.from(0, TimeUnit.MILLISECONDS).toInstant()));

Edit:

, , /, . :

ZoneId ect = ZoneId.of(ZoneId.SHORT_IDS.get("ECT"));

DateTimeFormatter f1 = DateTimeFormatter.RFC_1123_DATE_TIME;
DateTimeFormatter f2 = f1.withZone(ect);
DateTimeFormatter f3 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
DateTimeFormatter f4 = f3.withZone(ect);

LocalDateTime ldt = LocalDateTime.of(2015, 07, 21, 0, 0, 0, 0);
ZonedDateTime zdt = ZonedDateTime.of(ldt, ect);
Instant ins = zdt.toInstant();

System.out.println(f1.format(ins)); // throws exception (1)
System.out.println(f2.format(ins)); // Tue, 21 Jul 2015 00:00:00 +0200
System.out.println(f3.format(ins)); // throws exception (2)
System.out.println(f4.format(ins)); // 2015-07-21T00:00:00

System.out.println(f1.format(zdt)); // Tue, 21 Jul 2015 00:00:00 +0200
System.out.println(f2.format(zdt)); // Tue, 21 Jul 2015 00:00:00 +0200
System.out.println(f3.format(zdt)); // 2015-07-21T00:00:00
System.out.println(f4.format(zdt)); // 2015-07-21T00:00:00

System.out.println(f1.format(ldt)); // throws exception (3)
System.out.println(f2.format(ldt)); // throws exception (4)
System.out.println(f3.format(ldt)); // 2015-07-21T00:00:00
System.out.println(f4.format(ldt)); // 2015-07-21T00:00:00

ZoneId hst = ZoneId.of(ZoneId.SHORT_IDS.get("HST"));
ZonedDateTime zdt2 = ZonedDateTime.of(ldt, hst);

System.out.println(f1.format(zdt2)); // Tue, 21 Jul 2015 00:00:00 -1000
System.out.println(f2.format(zdt2)); // Tue, 21 Jul 2015 12:00:00 +0200
System.out.println(f3.format(zdt2)); // 2015-07-21T00:00:00
System.out.println(f4.format(zdt2)); // 2015-07-21T12:00:00
  • Instant , , . (1) (2) , , .
  • ZonedDateTime , . , . , .
  • LocalDateTime . , , , 1 . , ( ). (3) (4) , , .

, . , .

+7

1970 " ", Instant Instant.ofEpochMilli(long) long . (long + int ) " Java", - JSR-310 .

FileTime fromMillis(long) .

- , Sleafar, .

+1

Instant . . ISO_INSTANT RFC_1123_DATE_TIME, :

        inst = Instant.now();
        System.out.println(java.time.format.DateTimeFormatter.ISO_INSTANT
                           .format( inst ) );

- > 2015-07-20T21:11: 53.001Z

If you really want to have the RFC_1123 format, you need to declare a time zone.

Or add it to the formatter:

        System.out.println(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME
                           .withZone( ZoneOffset.UTC )
                           .format( inst ) );

or convert Instant to ZonedDateTime:

        ZonedDateTime zdt = ZonedDateTime.ofInstant( inst, ZoneOffset.UTC );
        System.out.println(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME
                           .format( zdt ) );

-> Mon, July 20, 2015 9:11:53 PM GMT

+1
source

Source: https://habr.com/ru/post/1598756/


All Articles