Top Description Fields Constructors Methods
org.threeten.extra

public final Class Seconds

extends Object
implements TemporalAmount, Comparable<Seconds>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable, java.time.temporal.TemporalAmount
Static Imports
java.time.temporal.ChronoUnit.SECONDS

A second-based amount of time, such as '8 seconds'.

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 ==.

Field Summary

Modifier and TypeField and Description
private static final Pattern
PATTERN

The pattern for parsing.

private final int
seconds

The number of seconds.

private static final int
SECONDS_PER_DAY

The number of seconds per day.

private static final int
SECONDS_PER_HOUR

The number of seconds per hour.

private static final int
SECONDS_PER_MINUTE

The number of seconds per minute.

private static final long
serialVersionUID

A serialization identifier for this class.

public static final Seconds
ZERO

A constant for zero seconds.

Constructor Summary

AccessConstructor and Description
private
Seconds(int
the amount of seconds
seconds
)

Constructs an instance using a specific number of seconds.

Method Summary

Modifier and TypeMethod and Description
public Seconds

Returns:

the absolute amount, not null
abs
()

Returns a copy of this duration with a positive length.

public Temporal

Returns:

an object of the same type with the adjustment made, not null
addTo
(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.temporal.TemporalAmount.addTo.

Adds this amount to the specified temporal object.

public static Seconds

Returns:

the number of seconds between the start and end temporals, not null
between
(Temporal
the start temporal, inclusive, not null
startInclusive
,
Temporal
the end temporal, exclusive, not null
endExclusive
)

Obtains a Seconds consisting of the number of seconds between two temporals.

public int

Returns:

the comparator value, negative if less, positive if greater
compareTo
(Seconds
the other amount, not null
otherAmount
)

Implements java.lang.Comparable.compareTo.

Compares this amount to the specified Seconds.

public Seconds

Returns:

the amount divided by the specified divisor, not null
dividedBy
(int
the amount to divide by, may be negative
divisor
)

Returns an instance with the amount divided by the specified divisor.

public boolean

Returns:

true if the other amount is equal to this one
equals
(Object
the other amount, null returns false
otherAmount
)

Overrides java.lang.Object.equals.

Checks if this amount is equal to the specified Seconds.

public static Seconds

Returns:

the equivalent amount, not null
from
(TemporalAmount
the temporal amount to convert, not null
amount
)

Obtains an instance of Seconds from a temporal amount.

public long

Returns:

the long value of the unit
get
(TemporalUnit
the TemporalUnit for which to return the value
unit
)

Implements java.time.temporal.TemporalAmount.get.

Gets the value of the requested unit.

public int

Returns:

the number of seconds
getAmount
()

Gets the number of seconds in this amount.

public List<TemporalUnit>

Returns:

a list containing the seconds unit, not null
getUnits
()

Implements java.time.temporal.TemporalAmount.getUnits.

Gets the set of units supported by this amount.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

A hash code for this amount.

public boolean

Returns:

true if the amount is negative, false if the amount is zero or positive
isNegative
()

Checks if the amount is negative.

public boolean

Returns:

true if the amount is positive, false if the amount is zero or negative
isPositive
()

Checks if the amount is positive.

public boolean

Returns:

true if the amount is zero, false if not
isZero
()

Checks if the amount is zero.

public Seconds

Returns:

a Seconds based on this instance with the requested amount subtracted, not null
minus
(TemporalAmount
the amount to subtract, not null
amountToSubtract
)

Returns a copy of this amount with the specified amount subtracted.

public Seconds

Returns:

a Seconds based on this instance with the requested amount subtracted, not null
minus
(int
the amount of seconds to add, may be negative
seconds
)

Returns a copy of this amount with the specified number of seconds subtracted.

public Seconds

Returns:

the amount multiplied by the specified scalar, not null
multipliedBy
(int
the scalar to multiply by, not null
scalar
)

Returns an instance with the amount multiplied by the specified scalar.

public Seconds

Returns:

the negated amount, not null
negated
()

Returns an instance with the amount negated.

public static Seconds

Returns:

the number of seconds, not null
of
(int
the number of seconds, positive or negative
seconds
)

Obtains a Seconds representing a number of seconds.

public static Seconds

Returns:

the amount with the input hours converted to seconds, not null
ofHours
(int
the number of hours, positive or negative
hours
)

Obtains a Seconds representing the number of seconds equivalent to a number of hours.

public static Seconds

Returns:

the amount with the input minutes converted to seconds, not null
ofMinutes
(int
the number of minutes, positive or negative
minutes
)

Obtains a Seconds representing the number of seconds equivalent to a number of hours.

public static Seconds

Returns:

the parsed period, not null
parse
(CharSequence
the text to parse, not null
text
)

Obtains a Seconds from a text string such as PTnS.

public Seconds

Returns:

a Seconds based on this instance with the requested amount added, not null
plus
(TemporalAmount
the amount to add, not null
amountToAdd
)

Returns a copy of this amount with the specified amount added.

public Seconds

Returns:

a Seconds based on this instance with the requested amount added, not null
plus
(int
the amount of seconds to add, may be negative
seconds
)

Returns a copy of this amount with the specified number of seconds added.

private Object

Returns:

the singleton instance
readResolve
()

Resolves singletons.

public Temporal

Returns:

an object of the same type with the adjustment made, not null
subtractFrom
(Temporal
the temporal object to adjust, not null
temporal
)

Implements java.time.temporal.TemporalAmount.subtractFrom.

Subtracts this amount from the specified temporal object.

public Duration

Returns:

the equivalent duration, not null
toDuration
()

Gets the number of seconds as a Duration.

public String

Returns:

the number of seconds in ISO-8601 string format
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the number of seconds.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

PATTERNback to summary
private static final Pattern PATTERN

The pattern for parsing.

secondsback to summary
private final int seconds

The number of seconds.

SECONDS_PER_DAYback to summary
private static final int SECONDS_PER_DAY

The number of seconds per day.

SECONDS_PER_HOURback to summary
private static final int SECONDS_PER_HOUR

The number of seconds per hour.

SECONDS_PER_MINUTEback to summary
private static final int SECONDS_PER_MINUTE

The number of seconds per minute.

serialVersionUIDback to summary
private static final long serialVersionUID

A serialization identifier for this class.

ZEROback to summary
public static final Seconds ZERO

A constant for zero seconds.

Constructor Detail

Secondsback to summary
private Seconds(int seconds)

Constructs an instance using a specific number of seconds.

Parameters
seconds:int

the amount of seconds

Method Detail

absback to summary
public Seconds 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:Seconds

the absolute amount, not null

Exceptions
ArithmeticException:
if numeric overflow occurs, which only happens if the amount is Long.MIN_VALUE
addToback to summary
public Temporal addTo(Temporal temporal)

Implements java.time.temporal.TemporalAmount.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.

Parameters
temporal:Temporal

the temporal object to adjust, not null

Returns:Temporal

an object of the same type with the adjustment made, not null

Annotations
@Override
Exceptions
DateTimeException:
if unable to add
UnsupportedTemporalTypeException:
if the SECONDS unit is not supported
ArithmeticException:
if numeric overflow occurs
betweenback to summary
public static Seconds between(Temporal startInclusive, Temporal endExclusive)

Obtains a Seconds consisting 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:Temporal

the start temporal, inclusive, not null

endExclusive:Temporal

the end temporal, exclusive, not null

Returns:Seconds

the number of seconds between the start and end temporals, not null

compareToback to summary
public int compareTo(Seconds otherAmount)

Implements java.lang.Comparable.compareTo.

Compares this amount to the specified Seconds.

The comparison is based on the total length of the amounts. It is "consistent with equals", as defined by Comparable.

Parameters
otherAmount:Seconds

the other amount, not null

Returns:int

the comparator value, negative if less, positive if greater

Annotations
@Override
dividedByback to summary
public Seconds dividedBy(int divisor)

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:int

the amount to divide by, may be negative

Returns:Seconds

the amount divided by the specified divisor, not null

Exceptions
ArithmeticException:
if the divisor is zero
equalsback to summary
public boolean equals(Object otherAmount)

Overrides java.lang.Object.equals.

Checks if this amount is equal to the specified Seconds.

The comparison is based on the total length of the durations.

Parameters
otherAmount:Object

the other amount, null returns false

Returns:boolean

true if the other amount is equal to this one

Annotations
@Override
fromback to summary
public static Seconds from(TemporalAmount amount)

Obtains an instance of Seconds from a temporal amount.

This obtains an instance based on the specified amount. A TemporalAmount represents an amount of time, which may be date-based or time-based, which this factory extracts to a Seconds.

The result is calculated by looping around each unit in the specified amount. Each amount is converted to seconds using Temporals#convertAmount. If the conversion yields a remainder, an exception is thrown. If the amount is zero, the unit is ignored.

Parameters
amount:TemporalAmount

the temporal amount to convert, not null

Returns:Seconds

the equivalent amount, not null

Exceptions
DateTimeException:
if unable to convert to a Seconds
ArithmeticException:
if numeric overflow occurs
getback to summary
public long get(TemporalUnit unit)

Implements java.time.temporal.TemporalAmount.get.

Gets the value of the requested unit.

This returns a value for the supported unit - SECONDS. All other units throw an exception.

Parameters
unit:TemporalUnit

the TemporalUnit for which to return the value

Returns:long

the long value of the unit

Annotations
@Override
Exceptions
UnsupportedTemporalTypeException:
if the unit is not supported
getAmountback to summary
public int getAmount()

Gets the number of seconds in this amount.

Returns:int

the number of seconds

getUnitsback to summary
public List<TemporalUnit> getUnits()

Implements java.time.temporal.TemporalAmount.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.

Returns:List<TemporalUnit>

a list containing the seconds unit, not null

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

A hash code for this amount.

Returns:int

a suitable hash code

Annotations
@Override
isNegativeback to summary
public boolean isNegative()

Checks if the amount is negative.

Returns:boolean

true if the amount is negative, false if the amount is zero or positive

isPositiveback to summary
public boolean isPositive()

Checks if the amount is positive.

Returns:boolean

true if the amount is positive, false if the amount is zero or negative

isZeroback to summary
public boolean isZero()

Checks if the amount is zero.

Returns:boolean

true if the amount is zero, false if not

minusback to summary
public Seconds minus(TemporalAmount amountToSubtract)

Returns a copy of this amount with the specified amount subtracted.

The parameter is converted using Seconds#from(TemporalAmount).

This instance is immutable and unaffected by this method call.

Parameters
amountToSubtract:TemporalAmount

the amount to subtract, not null

Returns:Seconds

a Seconds based on this instance with the requested amount subtracted, not null

Exceptions
DateTimeException:
if the specified amount contains an invalid unit
ArithmeticException:
if numeric overflow occurs
minusback to summary
public Seconds minus(int seconds)

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:int

the amount of seconds to add, may be negative

Returns:Seconds

a Seconds based on this instance with the requested amount subtracted, not null

Exceptions
ArithmeticException:
if the result overflows an int
multipliedByback to summary
public Seconds multipliedBy(int scalar)

Returns an instance with the amount multiplied by the specified scalar.

This instance is immutable and unaffected by this method call.

Parameters
scalar:int

the scalar to multiply by, not null

Returns:Seconds

the amount multiplied by the specified scalar, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
negatedback to summary
public Seconds negated()

Returns an instance with the amount negated.

This instance is immutable and unaffected by this method call.

Returns:Seconds

the negated amount, not null

Exceptions
ArithmeticException:
if numeric overflow occurs, which only happens if the amount is Long.MIN_VALUE
ofback to summary
public static Seconds of(int seconds)

Obtains a Seconds representing a number of seconds.

The resulting amount will have the specified seconds.

Parameters
seconds:int

the number of seconds, positive or negative

Returns:Seconds

the number of seconds, not null

ofHoursback to summary
public static Seconds ofHours(int hours)

Obtains a Seconds representing 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:int

the number of hours, positive or negative

Returns:Seconds

the amount with the input hours converted to seconds, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
ofMinutesback to summary
public static Seconds ofMinutes(int minutes)

Obtains a Seconds representing 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:int

the number of minutes, positive or negative

Returns:Seconds

the amount with the input minutes converted to seconds, not null

Exceptions
ArithmeticException:
if numeric overflow occurs
parseback to summary
public static Seconds parse(CharSequence text)

Obtains a Seconds from a text string such as PTnS.

This will parse the string produced by toString() and other related formats based on ISO-8601 PnDTnHnMnS.

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:CharSequence

the text to parse, not null

Returns:Seconds

the parsed period, not null

Annotations
@FromString
Exceptions
DateTimeParseException:
if the text cannot be parsed to a period
plusback to summary
public Seconds plus(TemporalAmount amountToAdd)

Returns a copy of this amount with the specified amount added.

The parameter is converted using Seconds#from(TemporalAmount).

This instance is immutable and unaffected by this method call.

Parameters
amountToAdd:TemporalAmount

the amount to add, not null

Returns:Seconds

a Seconds based on this instance with the requested amount added, not null

Exceptions
DateTimeException:
if the specified amount contains an invalid unit
ArithmeticException:
if numeric overflow occurs
plusback to summary
public Seconds plus(int seconds)

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:int

the amount of seconds to add, may be negative

Returns:Seconds

a Seconds based on this instance with the requested amount added, not null

Exceptions
ArithmeticException:
if the result overflows an int
readResolveback to summary
private Object readResolve()

Resolves singletons.

Returns:Object

the singleton instance

subtractFromback to summary
public Temporal subtractFrom(Temporal temporal)

Implements java.time.temporal.TemporalAmount.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.

Parameters
temporal:Temporal

the temporal object to adjust, not null

Returns:Temporal

an object of the same type with the adjustment made, not null

Annotations
@Override
Exceptions
DateTimeException:
if unable to subtract
UnsupportedTemporalTypeException:
if the SECONDS unit is not supported
ArithmeticException:
if numeric overflow occurs
toDurationback to summary
public Duration toDuration()

Gets the number of seconds as a Duration.

This returns a duration with the same number of seconds.

Returns:Duration

the equivalent duration, not null

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of the number of seconds. This will be in the format 'PTnS' where n is the number of seconds.

Returns:String

the number of seconds in ISO-8601 string format

Annotations
@Override
@ToString