Java Code Examples for java.time.temporal.TemporalUnit#getDuration()

The following examples show how to use java.time.temporal.TemporalUnit#getDuration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PackedLocalTime.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static int truncatedTo(TemporalUnit unit, int packedTime) {
  if (unit == ChronoUnit.NANOS || unit == ChronoUnit.MILLIS) {
    return packedTime;
  }
  Duration unitDur = unit.getDuration();
  if (unitDur.getSeconds() > SECONDS_PER_DAY) {
    throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
  }

  int hour = PackedLocalTime.getHour(packedTime);
  int minute = PackedLocalTime.getMinute(packedTime);
  int second = PackedLocalTime.getSecond(packedTime);
  int milli = 0;

  if (unit == ChronoUnit.DAYS) {
    hour = 0;
    minute = 0;
    second = 0;
  } else if (unit == ChronoUnit.HALF_DAYS) {
    if (hour >= 12) {
      hour = 12;
    } else {
      hour = 0;
    }
    minute = 0;
    second = 0;
  } else if (unit == ChronoUnit.HOURS) {
    minute = 0;
    second = 0;
  } else if (unit == ChronoUnit.MINUTES) {
    second = 0;
  }
  return PackedLocalTime.create(hour, minute, second, milli);
}
 
Example 2
Source File: Instant.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 3
Source File: Duration.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Duration} truncated to the specified unit.
 * <p>
 * Truncating the duration returns a copy of the original with conceptual fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other ChronoUnits throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit the unit to truncate to, not null
 * @return a {@code Duration} based on this duration with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @since 9
 */
public Duration truncatedTo(TemporalUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (unit == ChronoUnit.SECONDS && (seconds >= 0 || nanos == 0)) {
        return new Duration(seconds, 0);
    } else if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur ;
    return plusNanos(result - nod);
}
 
Example 4
Source File: Instant.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 5
Source File: LocalTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncation returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
Example 6
Source File: Instant.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 7
Source File: LocalTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncation returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
Example 8
Source File: LocalTime.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncation returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
Example 9
Source File: Instant.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 10
Source File: Instant.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 11
Source File: Instant.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 12
Source File: Instant.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 13
Source File: LocalTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncating the time returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
Example 14
Source File: LocalTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncating the time returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
Example 15
Source File: Instant.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 16
Source File: LocalTime.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncation returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
Example 17
Source File: Instant.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 18
Source File: LocalTime.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncation returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}
 
Example 19
Source File: Instant.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Instant} truncated to the specified unit.
 * <p>
 * Truncating the instant returns a copy of the original with fields
 * smaller than the specified unit set to zero.
 * The fields are calculated on the basis of using a UTC offset as seen
 * in {@code toString}.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down to the nearest minute, setting the seconds and nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return an {@code Instant} based on this instant with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public Instant truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 20
Source File: LocalTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code LocalTime} with the time truncated.
 * <p>
 * Truncating the time returns a copy of the original time with fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 * will set the second-of-minute and nano-of-second field to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all supplied time units on {@link ChronoUnit} and
 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit  the unit to truncate to, not null
 * @return a {@code LocalTime} based on this time with the time truncated, not null
 * @throws DateTimeException if unable to truncate
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 */
public LocalTime truncatedTo(TemporalUnit unit) {
    if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = toNanoOfDay();
    return ofNanoOfDay((nod / dur) * dur);
}