@Nullable public static LocalDateTime longToLocalDateTimeUtc(@Nullable Long l) { if (l == null) { return null; } return Instant.ofEpochMilli(l).atZone(ZoneOffset.UTC).toLocalDateTime(); }
@Nullable public static LocalDateTime longToLocalDateTime(@Nullable Long l) { if (l == null) { return null; } return Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault()).toLocalDateTime(); }
/** * Obtains an instance of {@code LocalDateTime} 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 LocalDateTime}. * <p> * The conversion extracts and combines {@code LocalDate} and {@code LocalTime}. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDateTime::from}. * * @param temporal the temporal object to convert, not null * @return the local date-time, not null * @throws DateTimeException if unable to convert to a {@code LocalDateTime} */ public static LocalDateTime from(TemporalAccessor temporal) { if (temporal instanceof LocalDateTime) { return (LocalDateTime) temporal; } else if (temporal instanceof ZonedDateTime) { return ((ZonedDateTime) temporal).toLocalDateTime(); } try { LocalDate date = LocalDate.from(temporal); LocalTime time = LocalTime.from(temporal); return new LocalDateTime(date, time); } catch (DateTimeException ex) { throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " + temporal + ", type " + temporal.getClass().getName()); } }
/** * Obtains an instance of {@code LocalDateTime} 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 LocalDateTime}. * <p> * The conversion extracts and combines {@code LocalDate} and {@code LocalTime}. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDateTime::from}. * * @param temporal the temporal object to convert, not null * @return the local date-time, not null * @throws DateTimeException if unable to convert to a {@code LocalDateTime} */ public static LocalDateTime from(TemporalAccessor temporal) { if (temporal instanceof LocalDateTime) { return (LocalDateTime) temporal; } else if (temporal instanceof ZonedDateTime) { return ((ZonedDateTime) temporal).toLocalDateTime(); } try { LocalDate date = LocalDate.from(temporal); LocalTime time = LocalTime.from(temporal); return new LocalDateTime(date, time); } catch (DateTimeException ex) { throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " + temporal + ", type " + temporal.getClass().getName()); } }
/** * Returns a copy of this date-time changing the zone offset to the * later of the two valid offsets at a local time-line overlap. * <p> * This method only has any effect when the local time-line overlaps, such as * at an autumn daylight savings cutover. In this scenario, there are two * valid offsets for the local date-time. Calling this method will return * a zoned date-time with the later of the two selected. * <p> * If this method is called when it is not an overlap, {@code this} * is returned. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code ZonedDateTime} based on this date-time with the later offset, not null */ @Override public ZonedDateTime withLaterOffsetAtOverlap() { ZoneOffsetTransition trans = getZone().getRules().getTransition(toLocalDateTime()); if (trans != null) { ZoneOffset laterOffset = trans.getOffsetAfter(); if (laterOffset.equals(offset) == false) { return new ZonedDateTime(dateTime, laterOffset, zone); } } return this; }
/** * Returns a copy of this date-time changing the zone offset to the * later of the two valid offsets at a local time-line overlap. * <p> * This method only has any effect when the local time-line overlaps, such as * at an autumn daylight savings cutover. In this scenario, there are two * valid offsets for the local date-time. Calling this method will return * a zoned date-time with the later of the two selected. * <p> * If this method is called when it is not an overlap, {@code this} * is returned. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code ZonedDateTime} based on this date-time with the later offset, not null */ @Override public ZonedDateTime withLaterOffsetAtOverlap() { ZoneOffsetTransition trans = getZone().getRules().getTransition(toLocalDateTime()); if (trans != null) { ZoneOffset laterOffset = trans.getOffsetAfter(); if (laterOffset.equals(offset) == false) { return new ZonedDateTime(dateTime, laterOffset, zone); } } return this; }