```
1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
``` 1197 * @since 9 1198 */ 1199 public long toSeconds() { 1200 return seconds; 1201 } 1202 1203 /** 1204 * Converts this duration to the total length in milliseconds. 1205 * <p> 1206 * If this duration is too large to fit in a {@code long} milliseconds, then an 1207 * exception is thrown. 1208 * <p> 1209 * If this duration has greater than millisecond precision, then the conversion 1210 * will drop any excess precision information as though the amount in nanoseconds 1211 * was subject to integer division by one million. 1212 * 1213 * @return the total length of the duration in milliseconds 1214 * @throws ArithmeticException if numeric overflow occurs 1215 */ 1216 public long toMillis() { 1217 long millis = Math.multiplyExact(seconds, 1000); 1218 millis = Math.addExact(millis, nanos / 1000_000); 1219 return millis; 1220 } 1221 1222 /** 1223 * Converts this duration to the total length in nanoseconds expressed as a {@code long}. 1224 * <p> 1225 * If this duration is too large to fit in a {@code long} nanoseconds, then an 1226 * exception is thrown. 1227 * 1228 * @return the total length of the duration in nanoseconds 1229 * @throws ArithmeticException if numeric overflow occurs 1230 */ 1231 public long toNanos() { 1232 long totalNanos = Math.multiplyExact(seconds, NANOS_PER_SECOND); 1233 totalNanos = Math.addExact(totalNanos, nanos); 1234 return totalNanos; 1235 } 1236 1237 /** 1238 * Extracts the number of days in the duration. 1239 * <p> 1240 * This returns the total number of days in the duration by dividing the 1241 * number of seconds by 86400. 1242 * This is based on the standard definition of a day as 24 hours. 1243 * <p> 1244 * This instance is immutable and unaffected by this method call. 1245 * 1246 * @return the number of days in the duration, may be negative 1247 * @since 9 1248 */ 1249 public long toDaysPart(){ 1250 return seconds / SECONDS_PER_DAY; 1251 } 1252 1253 /** | ```
1 /*
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
``` 1197 * @since 9 1198 */ 1199 public long toSeconds() { 1200 return seconds; 1201 } 1202 1203 /** 1204 * Converts this duration to the total length in milliseconds. 1205 * <p> 1206 * If this duration is too large to fit in a {@code long} milliseconds, then an 1207 * exception is thrown. 1208 * <p> 1209 * If this duration has greater than millisecond precision, then the conversion 1210 * will drop any excess precision information as though the amount in nanoseconds 1211 * was subject to integer division by one million. 1212 * 1213 * @return the total length of the duration in milliseconds 1214 * @throws ArithmeticException if numeric overflow occurs 1215 */ 1216 public long toMillis() { 1217 long tempSeconds = seconds; 1218 long tempNanos = nanos; 1219 if (tempSeconds < 0) { 1220 // change the seconds and nano value to 1221 // handle Long.MIN_VALUE case 1222 tempSeconds = tempSeconds + 1; 1223 tempNanos = tempNanos - 1000_000_000L; 1224 } 1225 long millis = Math.multiplyExact(tempSeconds, 1000); 1226 millis = Math.addExact(millis, tempNanos / 1000_000); 1227 return millis; 1228 } 1229 1230 /** 1231 * Converts this duration to the total length in nanoseconds expressed as a {@code long}. 1232 * <p> 1233 * If this duration is too large to fit in a {@code long} nanoseconds, then an 1234 * exception is thrown. 1235 * 1236 * @return the total length of the duration in nanoseconds 1237 * @throws ArithmeticException if numeric overflow occurs 1238 */ 1239 public long toNanos() { 1240 long tempSeconds = seconds; 1241 long tempNanos = nanos; 1242 if (tempSeconds < 0) { 1243 // change the seconds and nano value to 1244 // handle Long.MIN_VALUE case 1245 tempSeconds = tempSeconds + 1; 1246 tempNanos = tempNanos - 1000_000_000L; 1247 } 1248 long totalNanos = Math.multiplyExact(tempSeconds, NANOS_PER_SECOND); 1249 totalNanos = Math.addExact(totalNanos, tempNanos); 1250 return totalNanos; 1251 } 1252 1253 /** 1254 * Extracts the number of days in the duration. 1255 * <p> 1256 * This returns the total number of days in the duration by dividing the 1257 * number of seconds by 86400. 1258 * This is based on the standard definition of a day as 24 hours. 1259 * <p> 1260 * This instance is immutable and unaffected by this method call. 1261 * 1262 * @return the number of days in the duration, may be negative 1263 * @since 9 1264 */ 1265 public long toDaysPart(){ 1266 return seconds / SECONDS_PER_DAY; 1267 } 1268 1269 /** |