How to convert LocalDateTime to GregorianCalendar?

Why is it possible to pass an object LocalDateTimeto a method ZonedDateTime.from()if it is impossible to parse a simple datetime, as follows?

@Test
public void test() throws Exception {
    GregorianCalendar.from(ZonedDateTime.from(LocalDateTime.parse("2016-08-31T23:00:59")));
}

Result:

java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2016-08-31T23:00:59 of type java.time.LocalDateTime
    at java.time.ZonedDateTime.from(ZonedDateTime.java:565)
+4
source share
2 answers

Why can I pass a LocalDateTime object to ZonedDateTime.from () if it is not possible to parse a simple date and time?

Both LocalDateTime and ZonedDateTime implements the Temporal interface , which extends TemporalAccessor .

LocalDateTime is a date-time without a time zone in the ISO-8601 calendar system, for example, 2007-12-03T10: 15: 30

ZonedDateTime - - ISO-8601, , 2007-12-03T10: 15: 30 + 01: 00 /.

, from(TemporalAccessor temporal), TemporalAccessor. ZonedDateTime, LocalDateTime TemporalAccessor (Temporal extends TemporalAccessor), ( ), , Zoned .

:

public static ZonedDateTime from(TemporalAccessor temporal) {
            if (temporal instanceof ZonedDateTime) {
                return (ZonedDateTime) temporal;
            }
            try {
                ZoneId zone = ZoneId.from(temporal);
                if (temporal.isSupported(INSTANT_SECONDS)) {
                    long epochSecond = temporal.getLong(INSTANT_SECONDS);
                    int nanoOfSecond = temporal.get(NANO_OF_SECOND);
                    return create(epochSecond, nanoOfSecond, zone);
                } else {
                    LocalDate date = LocalDate.from(temporal);
                    LocalTime time = LocalTime.from(temporal);
                    return of(date, time, zone);
                }
            } catch (DateTimeException ex) {
                throw new DateTimeException("Unable to obtain ZonedDateTime from TemporalAccessor: " +
                        temporal + " of type " + temporal.getClass().getName(), ex);
            }
        }

.

LocalDateTime.parse("2016-08-31T23:00:59") from(TemporalAccessor temporal). , ZonedDateTime, ZoneId zone = ZoneId.from(temporal);

, , LocalDateTime .

?

ZonedDateTime

LocalDateTime ldt = LocalDateTime.parse("2016-08-31T23:00:59");
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zdt = ldt.atZone(zoneId);
GregorianCalendar gc = GregorianCalendar.from(zdt);

Junit

@Test
public void test() throws Exception {
    ZoneId zoneId = ZoneId.of("Europe/Paris");
    GregorianCalendar.from(LocalDateTime.parse("2016-08-31T23:00:59").atZone(zoneId));
}
+5

ZoneId, , , ,

LocalDateTime ldt = LocalDateTime.parse("2016-08-31T23:00:59");
GregorianCalendar gc = GregorianCalendar.from(ZonedDateTime.of(ldt, ZoneId.systemDefault()));
System.out.println(gc);

:

java.util.GregorianCalendar [ = 1472664659000, areFieldsSet = , areAllFieldsSet = , = , = sun.util.calendar.ZoneInfo [ID = "/", = 19800000, dstSavings = 0, useDaylight = , = 6, lastRule = NULL], firstDayOfWeek = 2, minimalDaysInFirstWeek = 4, ERA = 1, = 2016, = 7, WEEK_OF_YEAR = 35, WEEK_OF_MONTH = 5, DAY_OF_MONTH = 31, day_of_year = 244, DAY_OF_WEEK = 4, DAY_OF_WEEK_IN_MONTH = 5, AM_PM = 1, = 11, HOUR_OF_DAY = 23, = 0, = 59, = 0, ZONE_OFFSET = 19800000, DST_OFFSET = 0]

( ZonedDateTime TemporalAccessor )

Java 8 java.time. * - . - ZonedDateTime, , .

+4

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


All Articles