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
45 *
46 * * Neither the name of JSR-310 nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62 package java.time;
63
64 import static java.time.LocalTime.MINUTES_PER_HOUR;
65 import static java.time.LocalTime.NANOS_PER_SECOND;
66 import static java.time.LocalTime.SECONDS_PER_DAY;
67 import static java.time.LocalTime.SECONDS_PER_HOUR;
68 import static java.time.LocalTime.SECONDS_PER_MINUTE;
69 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
70 import static java.time.temporal.ChronoUnit.DAYS;
71 import static java.time.temporal.ChronoUnit.NANOS;
72 import static java.time.temporal.ChronoUnit.SECONDS;
73
74 import java.io.DataInput;
75 import java.io.DataOutput;
76 import java.io.IOException;
77 import java.io.InvalidObjectException;
78 import java.io.ObjectInputStream;
79 import java.io.Serializable;
80 import java.math.BigDecimal;
81 import java.math.BigInteger;
82 import java.math.RoundingMode;
83 import java.time.format.DateTimeParseException;
84 import java.time.temporal.ChronoField;
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
45 *
46 * * Neither the name of JSR-310 nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62 package java.time;
63
64 import static java.time.LocalTime.MINUTES_PER_HOUR;
65 import static java.time.LocalTime.NANOS_PER_MILLI;
66 import static java.time.LocalTime.NANOS_PER_SECOND;
67 import static java.time.LocalTime.SECONDS_PER_DAY;
68 import static java.time.LocalTime.SECONDS_PER_HOUR;
69 import static java.time.LocalTime.SECONDS_PER_MINUTE;
70 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
71 import static java.time.temporal.ChronoUnit.DAYS;
72 import static java.time.temporal.ChronoUnit.NANOS;
73 import static java.time.temporal.ChronoUnit.SECONDS;
74
75 import java.io.DataInput;
76 import java.io.DataOutput;
77 import java.io.IOException;
78 import java.io.InvalidObjectException;
79 import java.io.ObjectInputStream;
80 import java.io.Serializable;
81 import java.math.BigDecimal;
82 import java.math.BigInteger;
83 import java.math.RoundingMode;
84 import java.time.format.DateTimeParseException;
85 import java.time.temporal.ChronoField;
1198 * @since 9
1199 */
1200 public long toSeconds() {
1201 return seconds;
1202 }
1203
1204 /**
1205 * Converts this duration to the total length in milliseconds.
1206 * <p>
1207 * If this duration is too large to fit in a {@code long} milliseconds, then an
1208 * exception is thrown.
1209 * <p>
1210 * If this duration has greater than millisecond precision, then the conversion
1211 * will drop any excess precision information as though the amount in nanoseconds
1212 * was subject to integer division by one million.
1213 *
1214 * @return the total length of the duration in milliseconds
1215 * @throws ArithmeticException if numeric overflow occurs
1216 */
1217 public long toMillis() {
1218 long tempSeconds = seconds;
1219 long tempNanos = nanos;
1220 if (tempSeconds < 0) {
1221 // change the seconds and nano value to
1222 // handle Long.MIN_VALUE case
1223 tempSeconds = tempSeconds + 1;
1224 tempNanos = tempNanos - NANOS_PER_SECOND;
1225 }
1226 long millis = Math.multiplyExact(tempSeconds, 1000);
1227 millis = Math.addExact(millis, tempNanos / NANOS_PER_MILLI);
1228 return millis;
1229 }
1230
1231 /**
1232 * Converts this duration to the total length in nanoseconds expressed as a {@code long}.
1233 * <p>
1234 * If this duration is too large to fit in a {@code long} nanoseconds, then an
1235 * exception is thrown.
1236 *
1237 * @return the total length of the duration in nanoseconds
1238 * @throws ArithmeticException if numeric overflow occurs
1239 */
1240 public long toNanos() {
1241 long tempSeconds = seconds;
1242 long tempNanos = nanos;
1243 if (tempSeconds < 0) {
1244 // change the seconds and nano value to
1245 // handle Long.MIN_VALUE case
1246 tempSeconds = tempSeconds + 1;
1247 tempNanos = tempNanos - NANOS_PER_SECOND;
1248 }
1249 long totalNanos = Math.multiplyExact(tempSeconds, NANOS_PER_SECOND);
1250 totalNanos = Math.addExact(totalNanos, tempNanos);
1251 return totalNanos;
1252 }
1253
1254 /**
1255 * Extracts the number of days in the duration.
1256 * <p>
1257 * This returns the total number of days in the duration by dividing the
1258 * number of seconds by 86400.
1259 * This is based on the standard definition of a day as 24 hours.
1260 * <p>
1261 * This instance is immutable and unaffected by this method call.
1262 *
1263 * @return the number of days in the duration, may be negative
1264 * @since 9
1265 */
1266 public long toDaysPart(){
1267 return seconds / SECONDS_PER_DAY;
1268 }
1269
1270 /**
|