return; long lastModified = LocalDateTime.ofInstant(lastModifiedObj.toInstant(), ZoneId.of(ZoneOffset.systemDefault().getId())).toEpochSecond(ZoneOffset.UTC); String contentType = "video/mp4";
int cmp = toLocalDate().compareTo(other.toLocalDate()); if (cmp == 0) { cmp = toLocalTime().compareTo(other.toLocalTime()); if (cmp == 0) { cmp = getChronology().compareTo(other.getChronology());
static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException { ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject(); ZoneOffset offset = (ZoneOffset) in.readObject(); ZoneId zone = (ZoneId) in.readObject(); return dateTime.atZone(offset).withZoneSameLocal(zone); // TODO: ZDT uses ofLenient() }
package time; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalField; /** * Created by Michael * Creation date 4/24/2016. * @link */ public class TimeDemo { public static void main(String[] args) { // Three three lines do the work LocalDateTime localDateTime = LocalDateTime.of(2016, 4, 24, 9, 10); LocalDateTime halfHourLater = localDateTime.plusMinutes(30); // Add 30 minutes java.sql.Time sqlDateTime = new java.sql.Time(halfHourLater.toInstant(ZoneOffset.UTC).toEpochMilli()); // // Printout just to check System.out.println(DateTimeFormatter.ofPattern("yyyy-MMM-dd hh:mm:ss.SSS").format(halfHourLater)); System.out.println("java.time milliseconds: " + halfHourLater.toInstant(ZoneOffset.UTC).toEpochMilli()); System.out.println("System.currentMillis : " + System.currentTimeMillis()); } }
/** * Converts this date-time to an {@code Instant}. * <p> * This combines this local date-time and the specified offset to form * an {@code Instant}. * * @param offset the offset to use for the conversion, not null * @return an {@code Instant} representing the same instant, not null */ public Instant toInstant(ZoneOffset offset) { return Instant.ofEpochSecond(toEpochSecond(offset), toLocalTime().getNano()); }
@Override public long until(Temporal endExclusive, TemporalUnit unit) { @SuppressWarnings("unchecked") ChronoLocalDateTime<D> end = (ChronoLocalDateTime<D>) toLocalDate().getChronology().localDateTime(endExclusive); if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; if (f.isTimeBased()) { long amount = end.getLong(EPOCH_DAY) - date.getLong(EPOCH_DAY); switch (f) { case NANOS: amount = Jdk8Methods.safeMultiply(amount, NANOS_PER_DAY); break; case MICROS: amount = Jdk8Methods.safeMultiply(amount, MICROS_PER_DAY); break; case MILLIS: amount = Jdk8Methods.safeMultiply(amount, MILLIS_PER_DAY); break; case SECONDS: amount = Jdk8Methods.safeMultiply(amount, SECONDS_PER_DAY); break; case MINUTES: amount = Jdk8Methods.safeMultiply(amount, MINUTES_PER_DAY); break; case HOURS: amount = Jdk8Methods.safeMultiply(amount, HOURS_PER_DAY); break; case HALF_DAYS: amount = Jdk8Methods.safeMultiply(amount, 2); break; } return Jdk8Methods.safeAdd(amount, time.until(end.toLocalTime(), unit)); } ChronoLocalDate endDate = end.toLocalDate(); if (end.toLocalTime().isBefore(time)) { endDate = endDate.minus(1, ChronoUnit.DAYS); } return date.until(endDate, unit); } return unit.between(this, end); }
/** * Checks if this date-time is equal to another date-time, including the chronology. * <p> * Compares this date-time with another ensuring that the date-time and chronology are the same. * * @param obj the object to check, null returns false * @return true if this is equal to the other date */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof ChronoLocalDateTime) { return compareTo((ChronoLocalDateTime<?>) obj) == 0; } return false; }
return (field.isTimeBased() ? time.get(field) : date.get(field)); return super.get(field);
/** * Adjusts the specified temporal object to have the same date and time as this object. * <p> * This returns a temporal object of the same observable type as the input * with the date and time changed to be the same as this. * <p> * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} * twice, passing {@link ChronoField#EPOCH_DAY} and * {@link ChronoField#NANO_OF_DAY} as the fields. * <p> * In most cases, it is clearer to reverse the calling pattern by using * {@link Temporal#with(TemporalAdjuster)}: * <pre> * // these two lines are equivalent, but the second approach is recommended * temporal = thisLocalDateTime.adjustInto(temporal); * temporal = temporal.with(thisLocalDateTime); * </pre> * <p> * This instance is immutable and unaffected by this method call. * * @param temporal the target object to be adjusted, not null * @return the adjusted object, not null * @throws DateTimeException if unable to make the adjustment * @throws ArithmeticException if numeric overflow occurs */ @Override // override for Javadoc public Temporal adjustInto(Temporal temporal) { return super.adjustInto(temporal); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().getLong(field); } return field.getFrom(this); }
/** * Outputs this date-time as a {@code String} using the formatter. * <p> * This date-time will be passed to the formatter * {@link DateTimeFormatter#format(TemporalAccessor) print method}. * * @param formatter the formatter to use, not null * @return the formatted date-time string, not null * @throws DateTimeException if an error occurs during printing */ @Override // override for Javadoc public String format(DateTimeFormatter formatter) { return super.format(formatter); }
@SuppressWarnings("unchecked") @Override public <R> R query(TemporalQuery<R> query) { if (query == TemporalQueries.chronology()) { return (R) getChronology(); } else if (query == TemporalQueries.precision()) { return (R) NANOS; } else if (query == TemporalQueries.localDate()) { return (R) LocalDate.ofEpochDay(toLocalDate().toEpochDay()); } else if (query == TemporalQueries.localTime()) { return (R) toLocalTime(); } else if (query == TemporalQueries.zone() || query == TemporalQueries.zoneId() || query == TemporalQueries.offset()) { return null; } return super.query(query); }
@ms public static long toMillis(@Nonnull ChronoLocalDateTime<LocalDate> localDateTime) { return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); }
/** * Compares this date-time to another date-time. * <p> * The comparison is primarily based on the date-time, from earliest to latest. * It is "consistent with equals", as defined by {@link Comparable}. * <p> * If all the date-times being compared are instances of {@code LocalDateTime}, * then the comparison will be entirely based on the date-time. * If some dates being compared are in different chronologies, then the * chronology is also considered, see {@link ChronoLocalDateTime#compareTo}. * * @param other the other date-time to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override // override for Javadoc and performance public int compareTo(ChronoLocalDateTime<?> other) { if (other instanceof LocalDateTime) { return compareTo0((LocalDateTime) other); } return super.compareTo(other); }
@Override public int get(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new UnsupportedTemporalTypeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return toLocalDateTime().get(field); } return super.get(field); }
private void resolveInstant() { if (date != null && time != null) { if (zone != null) { long instant = date.atTime(time).atZone(zone).getLong(ChronoField.INSTANT_SECONDS); fieldValues.put(INSTANT_SECONDS, instant); } else { Long offsetSecs = fieldValues.get(OFFSET_SECONDS); if (offsetSecs != null) { ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue()); long instant = date.atTime(time).atZone(offset).getLong(ChronoField.INSTANT_SECONDS); fieldValues.put(INSTANT_SECONDS, instant); } } } }
public static RandomDate before(ChronoLocalDateTime before) { return before(before.toInstant(systemOffset())); } public static RandomDate before(ChronoZonedDateTime before) {
return; long lastModified = LocalDateTime.ofInstant(lastModifiedObj.toInstant(), ZoneId.of(ZoneOffset.systemDefault().getId())).toEpochSecond(ZoneOffset.UTC); String contentType = "video/mp4";
/** * Compares this date-time to another date-time, including the chronology. * <p> * The comparison is based first on the instant, then on the local date-time, * then on the zone ID, then on the chronology. * It is "consistent with equals", as defined by {@link Comparable}. * <p> * If all the date-time objects being compared are in the same chronology, then the * additional chronology stage is not required. * * @param other the other date-time to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override public int compareTo(ChronoZonedDateTime<?> other) { int cmp = Jdk8Methods.compareLongs(toEpochSecond(), other.toEpochSecond()); if (cmp == 0) { cmp = toLocalTime().getNano() - other.toLocalTime().getNano(); if (cmp == 0) { cmp = toLocalDateTime().compareTo(other.toLocalDateTime()); if (cmp == 0) { cmp = getZone().getId().compareTo(other.getZone().getId()); if (cmp == 0) { cmp = toLocalDate().getChronology().compareTo(other.toLocalDate().getChronology()); } } } } return cmp; }