< prev index next >
src/java.base/share/classes/java/time/Duration.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 60,69 ****
--- 60,70 ----
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time;
import static java.time.LocalTime.MINUTES_PER_HOUR;
+ import static java.time.LocalTime.NANOS_PER_MILLI;
import static java.time.LocalTime.NANOS_PER_SECOND;
import static java.time.LocalTime.SECONDS_PER_DAY;
import static java.time.LocalTime.SECONDS_PER_HOUR;
import static java.time.LocalTime.SECONDS_PER_MINUTE;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
*** 1212,1223 ****
*
* @return the total length of the duration in milliseconds
* @throws ArithmeticException if numeric overflow occurs
*/
public long toMillis() {
! long millis = Math.multiplyExact(seconds, 1000);
! millis = Math.addExact(millis, nanos / 1000_000);
return millis;
}
/**
* Converts this duration to the total length in nanoseconds expressed as a {@code long}.
--- 1213,1232 ----
*
* @return the total length of the duration in milliseconds
* @throws ArithmeticException if numeric overflow occurs
*/
public long toMillis() {
! long tempSeconds = seconds;
! long tempNanos = nanos;
! if (tempSeconds < 0) {
! // change the seconds and nano value to
! // handle Long.MIN_VALUE case
! tempSeconds = tempSeconds + 1;
! tempNanos = tempNanos - NANOS_PER_SECOND;
! }
! long millis = Math.multiplyExact(tempSeconds, 1000);
! millis = Math.addExact(millis, tempNanos / NANOS_PER_MILLI);
return millis;
}
/**
* Converts this duration to the total length in nanoseconds expressed as a {@code long}.
*** 1227,1238 ****
*
* @return the total length of the duration in nanoseconds
* @throws ArithmeticException if numeric overflow occurs
*/
public long toNanos() {
! long totalNanos = Math.multiplyExact(seconds, NANOS_PER_SECOND);
! totalNanos = Math.addExact(totalNanos, nanos);
return totalNanos;
}
/**
* Extracts the number of days in the duration.
--- 1236,1255 ----
*
* @return the total length of the duration in nanoseconds
* @throws ArithmeticException if numeric overflow occurs
*/
public long toNanos() {
! long tempSeconds = seconds;
! long tempNanos = nanos;
! if (tempSeconds < 0) {
! // change the seconds and nano value to
! // handle Long.MIN_VALUE case
! tempSeconds = tempSeconds + 1;
! tempNanos = tempNanos - NANOS_PER_SECOND;
! }
! long totalNanos = Math.multiplyExact(tempSeconds, NANOS_PER_SECOND);
! totalNanos = Math.addExact(totalNanos, tempNanos);
return totalNanos;
}
/**
* Extracts the number of days in the duration.
< prev index next >