Class Seconds
- All Implemented Interfaces:
Serializable,Comparable<Seconds>,TemporalAmount
This class models a quantity or amount of time in terms of seconds.
It is a type-safe way of representing a number of seconds in an application.
Note that Duration also models time in terms of seconds, but that
class allows nanoseconds, which this class does not.
The model is of a directed amount, meaning that the amount may be negative.
Implementation Requirements:
This class is immutable and thread-safe.This class must be treated as a value type. Do not synchronize, rely on the identity hash code or use the distinction between equals() and ==.
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns a copy of this duration with a positive length.Adds this amount to the specified temporal object.static SecondsObtains aSecondsconsisting of the number of seconds between two temporals.intCompares this amount to the specifiedSeconds.dividedBy(int divisor) Returns an instance with the amount divided by the specified divisor.booleanChecks if this amount is equal to the specifiedSeconds.static Secondsfrom(TemporalAmount amount) Obtains an instance ofSecondsfrom a temporal amount.longget(TemporalUnit unit) Gets the value of the requested unit.intGets the number of seconds in this amount.getUnits()Gets the set of units supported by this amount.inthashCode()A hash code for this amount.booleanChecks if the amount is negative.booleanChecks if the amount is positive.booleanisZero()Checks if the amount is zero.minus(int seconds) Returns a copy of this amount with the specified number of seconds subtracted.minus(TemporalAmount amountToSubtract) Returns a copy of this amount with the specified amount subtracted.multipliedBy(int scalar) Returns an instance with the amount multiplied by the specified scalar.negated()Returns an instance with the amount negated.static Secondsof(int seconds) Obtains aSecondsrepresenting a number of seconds.static SecondsofHours(int hours) Obtains aSecondsrepresenting the number of seconds equivalent to a number of hours.static SecondsofMinutes(int minutes) Obtains aSecondsrepresenting the number of seconds equivalent to a number of hours.static Secondsparse(CharSequence text) Obtains aSecondsfrom a text string such asPTnS.plus(int seconds) Returns a copy of this amount with the specified number of seconds added.plus(TemporalAmount amountToAdd) Returns a copy of this amount with the specified amount added.subtractFrom(Temporal temporal) Subtracts this amount from the specified temporal object.Gets the number of seconds as aDuration.toString()Returns a string representation of the number of seconds.
-
Field Details
-
ZERO
A constant for zero seconds.
-
-
Method Details
-
of
Obtains aSecondsrepresenting a number of seconds.The resulting amount will have the specified seconds.
- Parameters:
seconds- the number of seconds, positive or negative- Returns:
- the number of seconds, not null
-
ofHours
Obtains aSecondsrepresenting the number of seconds equivalent to a number of hours.The resulting amount will be second-based, with the number of seconds equal to the number of hours multiplied by 3600.
- Parameters:
hours- the number of hours, positive or negative- Returns:
- the amount with the input hours converted to seconds, not null
- Throws:
ArithmeticException- if numeric overflow occurs
-
ofMinutes
Obtains aSecondsrepresenting the number of seconds equivalent to a number of hours.The resulting amount will be second-based, with the number of seconds equal to the number of minutes multiplied by 60.
- Parameters:
minutes- the number of minutes, positive or negative- Returns:
- the amount with the input minutes converted to seconds, not null
- Throws:
ArithmeticException- if numeric overflow occurs
-
from
Obtains an instance ofSecondsfrom a temporal amount.This obtains an instance based on the specified amount. A
TemporalAmountrepresents an amount of time, which may be date-based or time-based, which this factory extracts to aSeconds.The result is calculated by looping around each unit in the specified amount. Each amount is converted to seconds using
Temporals.convertAmount(long, java.time.temporal.TemporalUnit, java.time.temporal.TemporalUnit). If the conversion yields a remainder, an exception is thrown. If the amount is zero, the unit is ignored.- Parameters:
amount- the temporal amount to convert, not null- Returns:
- the equivalent amount, not null
- Throws:
DateTimeException- if unable to convert to aSecondsArithmeticException- if numeric overflow occurs
-
parse
Obtains aSecondsfrom a text string such asPTnS.This will parse the string produced by
toString()and other related formats based on ISO-8601PnDTnHnMnS.The string starts with an optional sign, denoted by the ASCII negative or positive symbol. If negative, the whole amount is negated. The ASCII letter "P" is next in upper or lower case. There are four sections consisting of a number and a suffix. There is one section for days suffixed by "D", followed by one section for hours suffixed by "H", followed by one section for minutes suffixed by "M", followed by one section for seconds suffixed by "S". At least one section must be present. If the hours, minutes or seconds section is present it must be prefixed by "T". If the hours, minutes or seconds section is omitted the "T" must be omitted. Letters must be in ASCII upper or lower case. The number part of each section must consist of ASCII digits. The number may be prefixed by the ASCII negative or positive symbol. The number must parse to an
int.The leading plus/minus sign, and negative values for days, hours, minutes and seconds are not part of the ISO-8601 standard.
For example, the following are valid inputs:
"PT2S" -- Seconds.of(2) "PT-2S" -- Seconds.of(-2) "-PT2S" -- Seconds.of(-2) "-PT-2S" -- Seconds.of(2) "PT3S" -- Seconds.of(3 * 60) "PT3H-2M7S" -- Seconds.of(3 * 3600 - 2 * 60 + 7) "P2D" -- Seconds.of(2 * 86400) "P2DT3H" -- Seconds.of(2 * 86400 + 3 * 3600)
- Parameters:
text- the text to parse, not null- Returns:
- the parsed period, not null
- Throws:
DateTimeParseException- if the text cannot be parsed to a period
-
between
Obtains aSecondsconsisting of the number of seconds between two temporals.The start temporal is included, but the end temporal is not. The result of this method can be negative if the end is before the start.
- Parameters:
startInclusive- the start temporal, inclusive, not nullendExclusive- the end temporal, exclusive, not null- Returns:
- the number of seconds between the start and end temporals, not null
-
get
Gets the value of the requested unit.This returns a value for the supported unit -
SECONDS. All other units throw an exception.- Specified by:
getin interfaceTemporalAmount- Parameters:
unit- theTemporalUnitfor which to return the value- Returns:
- the long value of the unit
- Throws:
UnsupportedTemporalTypeException- if the unit is not supported
-
getUnits
Gets the set of units supported by this amount.The single supported unit is
SECONDS.This set can be used in conjunction with
get(TemporalUnit)to access the entire state of the amount.- Specified by:
getUnitsin interfaceTemporalAmount- Returns:
- a list containing the seconds unit, not null
-
getAmount
public int getAmount()Gets the number of seconds in this amount.- Returns:
- the number of seconds
-
isNegative
public boolean isNegative()Checks if the amount is negative.- Returns:
- true if the amount is negative, false if the amount is zero or positive
-
isZero
public boolean isZero()Checks if the amount is zero.- Returns:
- true if the amount is zero, false if not
-
isPositive
public boolean isPositive()Checks if the amount is positive.- Returns:
- true if the amount is positive, false if the amount is zero or negative
-
plus
Returns a copy of this amount with the specified amount added.The parameter is converted using
from(TemporalAmount).This instance is immutable and unaffected by this method call.
- Parameters:
amountToAdd- the amount to add, not null- Returns:
- a
Secondsbased on this instance with the requested amount added, not null - Throws:
DateTimeException- if the specified amount contains an invalid unitArithmeticException- if numeric overflow occurs
-
plus
Returns a copy of this amount with the specified number of seconds added.This instance is immutable and unaffected by this method call.
- Parameters:
seconds- the amount of seconds to add, may be negative- Returns:
- a
Secondsbased on this instance with the requested amount added, not null - Throws:
ArithmeticException- if the result overflows an int
-
minus
Returns a copy of this amount with the specified amount subtracted.The parameter is converted using
from(TemporalAmount).This instance is immutable and unaffected by this method call.
- Parameters:
amountToSubtract- the amount to subtract, not null- Returns:
- a
Secondsbased on this instance with the requested amount subtracted, not null - Throws:
DateTimeException- if the specified amount contains an invalid unitArithmeticException- if numeric overflow occurs
-
minus
Returns a copy of this amount with the specified number of seconds subtracted.This instance is immutable and unaffected by this method call.
- Parameters:
seconds- the amount of seconds to add, may be negative- Returns:
- a
Secondsbased on this instance with the requested amount subtracted, not null - Throws:
ArithmeticException- if the result overflows an int
-
multipliedBy
Returns an instance with the amount multiplied by the specified scalar.This instance is immutable and unaffected by this method call.
- Parameters:
scalar- the scalar to multiply by, not null- Returns:
- the amount multiplied by the specified scalar, not null
- Throws:
ArithmeticException- if numeric overflow occurs
-
dividedBy
Returns an instance with the amount divided by the specified divisor.The calculation uses integer division, thus 3 divided by 2 is 1.
This instance is immutable and unaffected by this method call.
- Parameters:
divisor- the amount to divide by, may be negative- Returns:
- the amount divided by the specified divisor, not null
- Throws:
ArithmeticException- if the divisor is zero
-
negated
Returns an instance with the amount negated.This instance is immutable and unaffected by this method call.
- Returns:
- the negated amount, not null
- Throws:
ArithmeticException- if numeric overflow occurs, which only happens if the amount isLong.MIN_VALUE
-
abs
Returns a copy of this duration with a positive length.This method returns a positive duration by effectively removing the sign from any negative total length.
This instance is immutable and unaffected by this method call.
- Returns:
- the absolute amount, not null
- Throws:
ArithmeticException- if numeric overflow occurs, which only happens if the amount isLong.MIN_VALUE
-
toDuration
Gets the number of seconds as aDuration.This returns a duration with the same number of seconds.
- Returns:
- the equivalent duration, not null
-
addTo
Adds this amount to the specified temporal object.This returns a temporal object of the same observable type as the input with this amount added.
In most cases, it is clearer to reverse the calling pattern by using
Temporal.plus(TemporalAmount).// these two lines are equivalent, but the second approach is recommended dateTime = thisAmount.addTo(dateTime); dateTime = dateTime.plus(thisAmount);
Only non-zero amounts will be added.
This instance is immutable and unaffected by this method call.
- Specified by:
addToin interfaceTemporalAmount- Parameters:
temporal- the temporal object to adjust, not null- Returns:
- an object of the same type with the adjustment made, not null
- Throws:
DateTimeException- if unable to addUnsupportedTemporalTypeException- if the SECONDS unit is not supportedArithmeticException- if numeric overflow occurs
-
subtractFrom
Subtracts this amount from the specified temporal object.This returns a temporal object of the same observable type as the input with this amount subtracted.
In most cases, it is clearer to reverse the calling pattern by using
Temporal.minus(TemporalAmount).// these two lines are equivalent, but the second approach is recommended dateTime = thisAmount.subtractFrom(dateTime); dateTime = dateTime.minus(thisAmount);
Only non-zero amounts will be subtracted.
This instance is immutable and unaffected by this method call.
- Specified by:
subtractFromin interfaceTemporalAmount- Parameters:
temporal- the temporal object to adjust, not null- Returns:
- an object of the same type with the adjustment made, not null
- Throws:
DateTimeException- if unable to subtractUnsupportedTemporalTypeException- if the SECONDS unit is not supportedArithmeticException- if numeric overflow occurs
-
compareTo
Compares this amount to the specifiedSeconds.The comparison is based on the total length of the amounts. It is "consistent with equals", as defined by
Comparable.- Specified by:
compareToin interfaceComparable<Seconds>- Parameters:
otherAmount- the other amount, not null- Returns:
- the comparator value, negative if less, positive if greater
-
equals
Checks if this amount is equal to the specifiedSeconds.The comparison is based on the total length of the durations.
-
hashCode
public int hashCode()A hash code for this amount. -
toString
Returns a string representation of the number of seconds. This will be in the format 'PTnS' where n is the number of seconds.
-