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

Print this page
rev 12809 : 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
Summary: Add new methods with long, int signatures.
Reviewed-by: XXX


 325      * @throws ArithmeticException if numeric overflow occurs
 326      */
 327     public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
 328         long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
 329         int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
 330         return create(secs, nos);
 331     }
 332 
 333     /**
 334      * Obtains an instance of {@code Instant} using milliseconds from the
 335      * epoch of 1970-01-01T00:00:00Z.
 336      * <p>
 337      * The seconds and nanoseconds are extracted from the specified milliseconds.
 338      *
 339      * @param epochMilli  the number of milliseconds from 1970-01-01T00:00:00Z
 340      * @return an instant, not null
 341      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
 342      */
 343     public static Instant ofEpochMilli(long epochMilli) {
 344         long secs = Math.floorDiv(epochMilli, 1000);
 345         int mos = (int)Math.floorMod(epochMilli, 1000);
 346         return create(secs, mos * 1000_000);
 347     }
 348 
 349     //-----------------------------------------------------------------------
 350     /**
 351      * Obtains an instance of {@code Instant} from a temporal object.
 352      * <p>
 353      * This obtains an instant based on the specified temporal.
 354      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 355      * which this factory converts to an instance of {@code Instant}.
 356      * <p>
 357      * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
 358      * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
 359      * <p>
 360      * This method matches the signature of the functional interface {@link TemporalQuery}
 361      * allowing it to be used as a query via method reference, {@code Instant::from}.
 362      *
 363      * @param temporal  the temporal object to convert, not null
 364      * @return the instant, not null
 365      * @throws DateTimeException if unable to convert to an {@code Instant}




 325      * @throws ArithmeticException if numeric overflow occurs
 326      */
 327     public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
 328         long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
 329         int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
 330         return create(secs, nos);
 331     }
 332 
 333     /**
 334      * Obtains an instance of {@code Instant} using milliseconds from the
 335      * epoch of 1970-01-01T00:00:00Z.
 336      * <p>
 337      * The seconds and nanoseconds are extracted from the specified milliseconds.
 338      *
 339      * @param epochMilli  the number of milliseconds from 1970-01-01T00:00:00Z
 340      * @return an instant, not null
 341      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
 342      */
 343     public static Instant ofEpochMilli(long epochMilli) {
 344         long secs = Math.floorDiv(epochMilli, 1000);
 345         int mos = Math.floorMod(epochMilli, 1000);
 346         return create(secs, mos * 1000_000);
 347     }
 348 
 349     //-----------------------------------------------------------------------
 350     /**
 351      * Obtains an instance of {@code Instant} from a temporal object.
 352      * <p>
 353      * This obtains an instant based on the specified temporal.
 354      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 355      * which this factory converts to an instance of {@code Instant}.
 356      * <p>
 357      * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
 358      * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
 359      * <p>
 360      * This method matches the signature of the functional interface {@link TemporalQuery}
 361      * allowing it to be used as a query via method reference, {@code Instant::from}.
 362      *
 363      * @param temporal  the temporal object to convert, not null
 364      * @return the instant, not null
 365      * @throws DateTimeException if unable to convert to an {@code Instant}