< prev index next >

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

Print this page




1177      * <p>
1178      * This returns the total number of minutes in the duration by dividing the
1179      * number of seconds by 60.
1180      * <p>
1181      * This instance is immutable and unaffected by this method call.
1182      *
1183      * @return the number of minutes in the duration, may be negative
1184      */
1185     public long toMinutes() {
1186         return seconds / SECONDS_PER_MINUTE;
1187     }
1188 
1189     /**
1190      * Gets the number of seconds in this duration.
1191      * <p>
1192      * This returns the total number of whole seconds in the duration.
1193      * <p>
1194      * This instance is immutable and unaffected by this method call.
1195      *
1196      * @return the whole seconds part of the length of the duration, positive or negative

1197      */
1198     public long toSeconds() {
1199         return seconds;
1200     }
1201 
1202     /**
1203      * Converts this duration to the total length in milliseconds.
1204      * <p>
1205      * If this duration is too large to fit in a {@code long} milliseconds, then an
1206      * exception is thrown.
1207      * <p>
1208      * If this duration has greater than millisecond precision, then the conversion
1209      * will drop any excess precision information as though the amount in nanoseconds
1210      * was subject to integer division by one million.
1211      *
1212      * @return the total length of the duration in milliseconds
1213      * @throws ArithmeticException if numeric overflow occurs
1214      */
1215     public long toMillis() {
1216         long millis = Math.multiplyExact(seconds, 1000);


1226      *
1227      * @return the total length of the duration in nanoseconds
1228      * @throws ArithmeticException if numeric overflow occurs
1229      */
1230     public long toNanos() {
1231         long totalNanos = Math.multiplyExact(seconds, NANOS_PER_SECOND);
1232         totalNanos = Math.addExact(totalNanos, nanos);
1233         return totalNanos;
1234     }
1235 
1236     /**
1237      * Extracts the number of days in the duration.
1238      * <p>
1239      * This returns the total number of days in the duration by dividing the
1240      * number of seconds by 86400.
1241      * This is based on the standard definition of a day as 24 hours.
1242      * <p>
1243      * This instance is immutable and unaffected by this method call.
1244      *
1245      * @return the number of days in the duration, may be negative

1246      */
1247     public long toDaysPart(){
1248         return seconds / SECONDS_PER_DAY;
1249     }
1250 
1251     /**
1252      * Extracts the number of hours part in the duration.
1253      * <p>
1254      * This returns the number of remaining hours when dividing {@link #toHours}
1255      * by hours in a day.
1256      * This is based on the standard definition of a day as 24 hours.
1257      * <p>
1258      * This instance is immutable and unaffected by this method call.
1259      *
1260      * @return the number of hours part in the duration, may be negative

1261      */
1262     public int toHoursPart(){
1263         return (int) (toHours() % 24);
1264     }
1265 
1266     /**
1267      * Extracts the number of minutes part in the duration.
1268      * <p>
1269      * This returns the number of remaining minutes when dividing {@link #toMinutes}
1270      * by minutes in an hour.
1271      * This is based on the standard definition of an hour as 60 minutes.
1272      * <p>
1273      * This instance is immutable and unaffected by this method call.
1274      *
1275      * @return the number of minutes parts in the duration, may be negative
1276      * may be negative
1277      */
1278     public int toMinutesPart(){
1279         return (int) (toMinutes() % MINUTES_PER_HOUR);
1280     }
1281 
1282     /**
1283      * Extracts the number of seconds part in the duration.
1284      * <p>
1285      * This returns the remaining seconds when dividing {@link #toSeconds}
1286      * by seconds in a minute.
1287      * This is based on the standard definition of a minute as 60 seconds.
1288      * <p>
1289      * This instance is immutable and unaffected by this method call.
1290      *
1291      * @return the number of seconds parts in the duration, may be negative

1292      */
1293     public int toSecondsPart(){
1294         return (int) (seconds % SECONDS_PER_MINUTE);
1295     }
1296 
1297     /**
1298      * Extracts the number of milliseconds part of the duration.
1299      * <p>
1300      * This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000.
1301      * The length of the duration is stored using two fields - seconds and nanoseconds.
1302      * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
1303      * the length in seconds.
1304      * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
1305      * <p>
1306      * This instance is immutable and unaffected by this method call.
1307      *
1308      * @return the number of milliseconds part of the duration.

1309      */
1310     public int toMillisPart(){
1311         return nanos / 1000_000;
1312     }
1313 
1314     /**
1315      * Get the nanoseconds part within seconds of the duration.
1316      * <p>
1317      * The length of the duration is stored using two fields - seconds and nanoseconds.
1318      * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
1319      * the length in seconds.
1320      * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
1321      * <p>
1322      * This instance is immutable and unaffected by this method call.
1323      *
1324      * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999

1325      */
1326     public int toNanosPart(){
1327         return nanos;
1328     }
1329 
1330     //-----------------------------------------------------------------------
1331     /**
1332      * Compares this duration to the specified {@code Duration}.
1333      * <p>
1334      * The comparison is based on the total length of the durations.
1335      * It is "consistent with equals", as defined by {@link Comparable}.
1336      *
1337      * @param otherDuration the other duration to compare to, not null
1338      * @return the comparator value, negative if less, positive if greater
1339      */
1340     @Override
1341     public int compareTo(Duration otherDuration) {
1342         int cmp = Long.compare(seconds, otherDuration.seconds);
1343         if (cmp != 0) {
1344             return cmp;




1177      * <p>
1178      * This returns the total number of minutes in the duration by dividing the
1179      * number of seconds by 60.
1180      * <p>
1181      * This instance is immutable and unaffected by this method call.
1182      *
1183      * @return the number of minutes in the duration, may be negative
1184      */
1185     public long toMinutes() {
1186         return seconds / SECONDS_PER_MINUTE;
1187     }
1188 
1189     /**
1190      * Gets the number of seconds in this duration.
1191      * <p>
1192      * This returns the total number of whole seconds in the duration.
1193      * <p>
1194      * This instance is immutable and unaffected by this method call.
1195      *
1196      * @return the whole seconds part of the length of the duration, positive or negative
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);


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     /**
1254      * Extracts the number of hours part in the duration.
1255      * <p>
1256      * This returns the number of remaining hours when dividing {@link #toHours}
1257      * by hours in a day.
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 hours part in the duration, may be negative
1263      * @since 9
1264      */
1265     public int toHoursPart(){
1266         return (int) (toHours() % 24);
1267     }
1268 
1269     /**
1270      * Extracts the number of minutes part in the duration.
1271      * <p>
1272      * This returns the number of remaining minutes when dividing {@link #toMinutes}
1273      * by minutes in an hour.
1274      * This is based on the standard definition of an hour as 60 minutes.
1275      * <p>
1276      * This instance is immutable and unaffected by this method call.
1277      *
1278      * @return the number of minutes parts in the duration, may be negative
1279      * @since 9
1280      */
1281     public int toMinutesPart(){
1282         return (int) (toMinutes() % MINUTES_PER_HOUR);
1283     }
1284 
1285     /**
1286      * Extracts the number of seconds part in the duration.
1287      * <p>
1288      * This returns the remaining seconds when dividing {@link #toSeconds}
1289      * by seconds in a minute.
1290      * This is based on the standard definition of a minute as 60 seconds.
1291      * <p>
1292      * This instance is immutable and unaffected by this method call.
1293      *
1294      * @return the number of seconds parts in the duration, may be negative
1295      * @since 9
1296      */
1297     public int toSecondsPart(){
1298         return (int) (seconds % SECONDS_PER_MINUTE);
1299     }
1300 
1301     /**
1302      * Extracts the number of milliseconds part of the duration.
1303      * <p>
1304      * This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000.
1305      * The length of the duration is stored using two fields - seconds and nanoseconds.
1306      * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
1307      * the length in seconds.
1308      * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
1309      * <p>
1310      * This instance is immutable and unaffected by this method call.
1311      *
1312      * @return the number of milliseconds part of the duration.
1313      * @since 9
1314      */
1315     public int toMillisPart(){
1316         return nanos / 1000_000;
1317     }
1318 
1319     /**
1320      * Get the nanoseconds part within seconds of the duration.
1321      * <p>
1322      * The length of the duration is stored using two fields - seconds and nanoseconds.
1323      * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
1324      * the length in seconds.
1325      * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
1326      * <p>
1327      * This instance is immutable and unaffected by this method call.
1328      *
1329      * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
1330      * @since 9
1331      */
1332     public int toNanosPart(){
1333         return nanos;
1334     }
1335 
1336     //-----------------------------------------------------------------------
1337     /**
1338      * Compares this duration to the specified {@code Duration}.
1339      * <p>
1340      * The comparison is based on the total length of the durations.
1341      * It is "consistent with equals", as defined by {@link Comparable}.
1342      *
1343      * @param otherDuration the other duration to compare to, not null
1344      * @return the comparator value, negative if less, positive if greater
1345      */
1346     @Override
1347     public int compareTo(Duration otherDuration) {
1348         int cmp = Long.compare(seconds, otherDuration.seconds);
1349         if (cmp != 0) {
1350             return cmp;


< prev index next >