try offset = ZoneOffset.from(temporal);
@Override public ZoneOffset queryFrom(TemporalAccessor temporal) { return ZoneOffset.from(temporal); } };
@Nullable public static OffsetDateTime parseDateTime(@Nullable String dtStr) { if (dtStr == null) { return null; } // An unspecified time zone is exactly that - unspecified. No more, no less. It's not saying it's in UTC, // nor is it saying it's in the local time zone or any other time zone, it's just saying that the time // read from some clock somewhere was that time // see http://stackoverflow.com/questions/20670041/what-is-the-default-time-zone-for-an-xml-schema-datetime-if-not-specified // => we use default time zone to the current time TemporalAccessor dt = DateTimeFormatter.ISO_DATE_TIME.parseBest(dtStr, OffsetDateTime::from, LocalDateTime::from); if (dt instanceof OffsetDateTime) { return (OffsetDateTime) dt; } else { return OffsetDateTime.of((LocalDateTime)dt, ZoneOffset.from(ZonedDateTime.now())); } }
@Nullable public static OffsetDateTime parseDate(@Nullable String dateStr) { if (dateStr == null) { return null; } // parsing has two steps: // 1) parse date with offset "2013-10-05+02:00" // 2) parse date without offset "2013-10-05" try { return OffsetDateTime.from(ISO_OFFSET_DATE_MIDNIGHT.parse(dateStr)); } catch (DateTimeParseException ex) { TemporalAccessor dt = DateTimeFormatter.ISO_DATE.parseBest(dateStr, OffsetDateTime::from, LocalDate::from); if (dt instanceof OffsetDateTime) { return (OffsetDateTime) dt; } else { LocalDateTime ldt = LocalDateTime.of((LocalDate)dt, LocalTime.now()); return OffsetDateTime.of(ldt.truncatedTo(ChronoUnit.DAYS), ZoneOffset.from(ZonedDateTime.now())); } } }
/** * Obtains an instance of {@code OffsetTime} from a temporal object. * <p> * A {@code TemporalAccessor} represents some form of date and time information. * This factory converts the arbitrary temporal object to an instance of {@code OffsetTime}. * <p> * The conversion extracts and combines {@code LocalTime} and {@code ZoneOffset}. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used in queries via method reference, {@code OffsetTime::from}. * * @param temporal the temporal object to convert, not null * @return the offset time, not null * @throws DateTimeException if unable to convert to an {@code OffsetTime} */ public static OffsetTime from(TemporalAccessor temporal) { if (temporal instanceof OffsetTime) { return (OffsetTime) temporal; } try { LocalTime time = LocalTime.from(temporal); ZoneOffset offset = ZoneOffset.from(temporal); return new OffsetTime(time, offset); } catch (DateTimeException ex) { throw new DateTimeException("Unable to obtain OffsetTime from TemporalAccessor: " + temporal + ", type " + temporal.getClass().getName()); } }
offset = ZoneOffset.from(temporal); } catch(final DateTimeException ignored) { offset = ZoneOffset.UTC;
ZoneOffset offset = ZoneOffset.from( temporal ); LocalDateTime ldt = LocalDateTime.from( temporal ); return ZonedDateTime.ofInstant( ldt, offset, zone );
ZoneOffset offset = ZoneOffset.from( temporal ); LocalDateTime ldt = LocalDateTime.from( temporal ); return ZonedDateTime.ofInstant( ldt, offset, zone );
ZoneOffset offset = ZoneOffset.from(temporal); try { LocalDateTime ldt = LocalDateTime.from(temporal);