@@ -42,6 +42,8 @@ import java.util.regex.Pattern
42
42
import org .threeten .bp .LocalTime .SECONDS_PER_DAY
43
43
import org .threeten .bp .LocalTime .SECONDS_PER_HOUR
44
44
import org .threeten .bp .LocalTime .SECONDS_PER_MINUTE
45
+ import org .threeten .bp .LocalTime .HOURS_PER_DAY
46
+ import org .threeten .bp .LocalTime .MINUTES_PER_HOUR
45
47
import org .threeten .bp .format .DateTimeParseException
46
48
import org .threeten .bp .temporal .ChronoField .NANO_OF_SECOND
47
49
import org .threeten .bp .temporal .ChronoUnit
@@ -925,7 +927,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
925
927
def multipliedBy (multiplicand : Long ): Duration =
926
928
if (multiplicand == 0 ) Duration .ZERO
927
929
else if (multiplicand == 1 ) this
928
- else Duration .create(toSeconds .multiply(BigDecimal .valueOf(multiplicand)))
930
+ else Duration .create(toSecondsBD .multiply(BigDecimal .valueOf(multiplicand)))
929
931
930
932
/**
931
933
* Returns a copy of this duration divided by the specified value.
@@ -944,7 +946,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
944
946
def dividedBy (divisor : Long ): Duration =
945
947
if (divisor == 0 ) throw new ArithmeticException (" Cannot divide by zero" )
946
948
else if (divisor == 1 ) this
947
- else Duration .create(toSeconds .divide(BigDecimal .valueOf(divisor), RoundingMode .DOWN ))
949
+ else Duration .create(toSecondsBD .divide(BigDecimal .valueOf(divisor), RoundingMode .DOWN ))
948
950
949
951
/**
950
952
* Converts this duration to the total length in seconds and fractional nanoseconds expressed as a
@@ -953,9 +955,37 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
953
955
* @return
954
956
* the total length of the duration in seconds, with a scale of 9, not null
955
957
*/
956
- private def toSeconds : BigDecimal =
958
+ private def toSecondsBD : BigDecimal =
957
959
BigDecimal .valueOf(seconds).add(BigDecimal .valueOf(nanos.toLong, 9 ))
958
960
961
+ /**
962
+ * Gets the number of seconds in this duration.
963
+ *
964
+ * This returns the total number of whole seconds in the duration.
965
+ *
966
+ * This instance is immutable and unaffected by this method call.
967
+ *
968
+ * @return
969
+ * the whole seconds part of the length of the duration, positive or negative
970
+ *
971
+ * @since 9
972
+ */
973
+ def toSeconds : Long = seconds
974
+
975
+ /**
976
+ * Extracts the number of seconds part in the duration.
977
+ *
978
+ * This returns the remaining seconds when dividing toSeconds() by seconds in a minute. This is
979
+ * based on the standard definition of a minute as 60 seconds.
980
+ *
981
+ * This instance is immutable and unaffected by this method call.
982
+ *
983
+ * @return
984
+ * the number of seconds parts in the duration, may be negative
985
+ * @since 9
986
+ */
987
+ def toSecondsPart : Int = (toSeconds % SECONDS_PER_MINUTE ).toInt
988
+
959
989
/**
960
990
* Returns a copy of this duration with the length negated.
961
991
*
@@ -1065,6 +1095,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
1065
1095
*/
1066
1096
def toDays : Long = seconds / SECONDS_PER_DAY
1067
1097
1098
+ /**
1099
+ * Extracts the number of days in the duration.
1100
+ *
1101
+ * This returns the total number of days in the duration by dividing the number of seconds by
1102
+ * 86400. This is based on the standard definition of a day as 24 hours.
1103
+ *
1104
+ * This instance is immutable and unaffected by this method call.
1105
+ *
1106
+ * @return
1107
+ * the number of days in the duration, may be negative
1108
+ * @since 9
1109
+ */
1110
+ def toDaysPart : Long = seconds / SECONDS_PER_DAY
1111
+
1068
1112
/**
1069
1113
* Gets the number of hours in this duration.
1070
1114
*
@@ -1078,6 +1122,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
1078
1122
*/
1079
1123
def toHours : Long = seconds / SECONDS_PER_HOUR
1080
1124
1125
+ /**
1126
+ * Extracts the number of hours part in the duration.
1127
+ *
1128
+ * This returns the number of remaining hours when dividing toHours() by hours in a day. This is
1129
+ * based on the standard definition of a day as 24 hours.
1130
+ *
1131
+ * This instance is immutable and unaffected by this method call.
1132
+ *
1133
+ * @return
1134
+ * the number of hours part in the duration, may be negative
1135
+ * @since 9
1136
+ */
1137
+ def toHoursPart : Int = (toHours % HOURS_PER_DAY ).toInt
1138
+
1081
1139
/**
1082
1140
* Gets the number of minutes in this duration.
1083
1141
*
@@ -1091,6 +1149,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
1091
1149
*/
1092
1150
def toMinutes : Long = seconds / SECONDS_PER_MINUTE
1093
1151
1152
+ /**
1153
+ * Extracts the number of minutes part in the duration.
1154
+ *
1155
+ * This returns the number of remaining minutes when dividing toMinutes() by minutes in an hour.
1156
+ * This is based on the standard definition of an hour as 60 minutes.
1157
+ *
1158
+ * This instance is immutable and unaffected by this method call.
1159
+ *
1160
+ * @return
1161
+ * the number of minutes parts in the duration, may be negative
1162
+ * @since 9
1163
+ */
1164
+ def toMinutesPart : Int = (toMinutes % MINUTES_PER_HOUR ).toInt
1165
+
1094
1166
/**
1095
1167
* Converts this duration to the total length in milliseconds.
1096
1168
*
@@ -1111,6 +1183,22 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
1111
1183
Math .addExact(result, nanos.toLong / Duration .NANOS_PER_MILLI )
1112
1184
}
1113
1185
1186
+ /**
1187
+ * Extracts the number of milliseconds part of the duration.
1188
+ *
1189
+ * This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000. The
1190
+ * length of the duration is stored using two fields - seconds and nanoseconds. The nanoseconds
1191
+ * part is a value from 0 to 999,999,999 that is an adjustment to the length in seconds. The total
1192
+ * duration is defined by calling getNano() and getSeconds().
1193
+ *
1194
+ * This instance is immutable and unaffected by this method call.
1195
+ *
1196
+ * @return
1197
+ * the number of milliseconds part of the duration.
1198
+ * @since 9
1199
+ */
1200
+ def toMillisPart : Int = (nanos / Duration .NANOS_PER_MILLI ).toInt
1201
+
1114
1202
/**
1115
1203
* Converts this duration to the total length in nanoseconds expressed as a {@code long}.
1116
1204
*
@@ -1127,6 +1215,21 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
1127
1215
Math .addExact(result, nanos.toLong)
1128
1216
}
1129
1217
1218
+ /**
1219
+ * Get the nanoseconds part within seconds of the duration.
1220
+ *
1221
+ * The length of the duration is stored using two fields - seconds and nanoseconds. The
1222
+ * nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to the length in
1223
+ * seconds. The total duration is defined by calling getNano() and getSeconds().
1224
+ *
1225
+ * This instance is immutable and unaffected by this method call.
1226
+ *
1227
+ * @return
1228
+ * the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
1229
+ * @since 9
1230
+ */
1231
+ def toNanosPart : Int = nanos
1232
+
1130
1233
/**
1131
1234
* Compares this duration to the specified {@code Duration}.
1132
1235
*
0 commit comments