< prev index next >

src/java.base/share/classes/java/time/Instant.java

Print this page




 741      * This instance is immutable and unaffected by this method call.
 742      *
 743      * @param unit  the unit to truncate to, not null
 744      * @return an {@code Instant} based on this instant with the time truncated, not null
 745      * @throws DateTimeException if the unit is invalid for truncation
 746      * @throws UnsupportedTemporalTypeException if the unit is not supported
 747      */
 748     public Instant truncatedTo(TemporalUnit unit) {
 749         if (unit == ChronoUnit.NANOS) {
 750             return this;
 751         }
 752         Duration unitDur = unit.getDuration();
 753         if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
 754             throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
 755         }
 756         long dur = unitDur.toNanos();
 757         if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
 758             throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
 759         }
 760         long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
 761         long result = (nod / dur) * dur;
 762         return plusNanos(result - nod);
 763     }
 764 
 765     //-----------------------------------------------------------------------
 766     /**
 767      * Returns a copy of this instant with the specified amount added.
 768      * <p>
 769      * This returns an {@code Instant}, based on this one, with the specified amount added.
 770      * The amount is typically {@link Duration} but may be any other type implementing
 771      * the {@link TemporalAmount} interface.
 772      * <p>
 773      * The calculation is delegated to the amount object by calling
 774      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
 775      * to implement the addition in any way it wishes, however it typically
 776      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
 777      * of the amount implementation to determine if it can be successfully added.
 778      * <p>
 779      * This instance is immutable and unaffected by this method call.
 780      *
 781      * @param amountToAdd  the amount to add, not null




 741      * This instance is immutable and unaffected by this method call.
 742      *
 743      * @param unit  the unit to truncate to, not null
 744      * @return an {@code Instant} based on this instant with the time truncated, not null
 745      * @throws DateTimeException if the unit is invalid for truncation
 746      * @throws UnsupportedTemporalTypeException if the unit is not supported
 747      */
 748     public Instant truncatedTo(TemporalUnit unit) {
 749         if (unit == ChronoUnit.NANOS) {
 750             return this;
 751         }
 752         Duration unitDur = unit.getDuration();
 753         if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
 754             throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
 755         }
 756         long dur = unitDur.toNanos();
 757         if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
 758             throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
 759         }
 760         long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
 761         long result = Math.floorDiv(nod, dur) * dur ;
 762         return plusNanos(result - nod);
 763     }
 764 
 765     //-----------------------------------------------------------------------
 766     /**
 767      * Returns a copy of this instant with the specified amount added.
 768      * <p>
 769      * This returns an {@code Instant}, based on this one, with the specified amount added.
 770      * The amount is typically {@link Duration} but may be any other type implementing
 771      * the {@link TemporalAmount} interface.
 772      * <p>
 773      * The calculation is delegated to the amount object by calling
 774      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
 775      * to implement the addition in any way it wishes, however it typically
 776      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
 777      * of the amount implementation to determine if it can be successfully added.
 778      * <p>
 779      * This instance is immutable and unaffected by this method call.
 780      *
 781      * @param amountToAdd  the amount to add, not null


< prev index next >