< prev index next >

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

Print this page

 102  * exactly equal to 24 hours, thus ignoring daylight savings effects.
 103  * See {@link Period} for the date-based equivalent to this class.
 104  * <p>
 105  * A physical duration could be of infinite length.
 106  * For practicality, the duration is stored with constraints similar to {@link Instant}.
 107  * The duration uses nanosecond resolution with a maximum value of the seconds that can
 108  * be held in a {@code long}. This is greater than the current estimated age of the universe.
 109  * <p>
 110  * The range of a duration requires the storage of a number larger than a {@code long}.
 111  * To achieve this, the class stores a {@code long} representing seconds and an {@code int}
 112  * representing nanosecond-of-second, which will always be between 0 and 999,999,999.
 113  * The model is of a directed duration, meaning that the duration may be negative.
 114  * <p>
 115  * The duration is measured in "seconds", but these are not necessarily identical to
 116  * the scientific "SI second" definition based on atomic clocks.
 117  * This difference only impacts durations measured near a leap-second and should not affect
 118  * most applications.
 119  * See {@link Instant} for a discussion as to the meaning of the second and time-scales.
 120  * <p>
 121  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 122  * class; programmers should treat instances that are
 123  * {@linkplain #equals(Object) equal} as interchangeable and should not
 124  * use instances for synchronization, or unpredictable behavior may
 125  * occur. For example, in a future release, synchronization may fail.
 126  * The {@code equals} method should be used for comparisons.







 127  *
 128  * @implSpec
 129  * This class is immutable and thread-safe.
 130  *
 131  * @since 1.8
 132  */
 133 @jdk.internal.ValueBased

 134 public final class Duration
 135         implements TemporalAmount, Comparable<Duration>, Serializable {
 136 
 137     /**
 138      * Constant for a duration of zero.
 139      */
 140     public static final Duration ZERO = new Duration(0, 0);
 141     /**
 142      * Serialization version.
 143      */
 144     @java.io.Serial
 145     private static final long serialVersionUID = 3078945930695997490L;
 146     /**
 147      * Constant for nanos per second.
 148      */
 149     private static final BigInteger BI_NANOS_PER_SECOND = BigInteger.valueOf(NANOS_PER_SECOND);
 150     /**
 151      * The pattern for parsing.
 152      */
 153     private static class Lazy {

 102  * exactly equal to 24 hours, thus ignoring daylight savings effects.
 103  * See {@link Period} for the date-based equivalent to this class.
 104  * <p>
 105  * A physical duration could be of infinite length.
 106  * For practicality, the duration is stored with constraints similar to {@link Instant}.
 107  * The duration uses nanosecond resolution with a maximum value of the seconds that can
 108  * be held in a {@code long}. This is greater than the current estimated age of the universe.
 109  * <p>
 110  * The range of a duration requires the storage of a number larger than a {@code long}.
 111  * To achieve this, the class stores a {@code long} representing seconds and an {@code int}
 112  * representing nanosecond-of-second, which will always be between 0 and 999,999,999.
 113  * The model is of a directed duration, meaning that the duration may be negative.
 114  * <p>
 115  * The duration is measured in "seconds", but these are not necessarily identical to
 116  * the scientific "SI second" definition based on atomic clocks.
 117  * This difference only impacts durations measured near a leap-second and should not affect
 118  * most applications.
 119  * See {@link Instant} for a discussion as to the meaning of the second and time-scales.
 120  * <p>
 121  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 122  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 123  * as interchangeable and should not use instances for synchronization, mutexes, or
 124  * with {@linkplain java.lang.ref.Reference object references}.
 125  *
 126  * <div class="preview-block">
 127  *      <div class="preview-comment">
 128  *          When preview features are enabled, {@code Duration} is a {@linkplain Class#isValue value class}.
 129  *          Use of value class instances for synchronization, mutexes, or with
 130  *          {@linkplain java.lang.ref.Reference object references} result in
 131  *          {@link IdentityException}.
 132  *      </div>
 133  * </div>
 134  *
 135  * @implSpec
 136  * This class is immutable and thread-safe.
 137  *
 138  * @since 1.8
 139  */
 140 @jdk.internal.ValueBased
 141 @jdk.internal.MigratedValueClass
 142 public final class Duration
 143         implements TemporalAmount, Comparable<Duration>, Serializable {
 144 
 145     /**
 146      * Constant for a duration of zero.
 147      */
 148     public static final Duration ZERO = new Duration(0, 0);
 149     /**
 150      * Serialization version.
 151      */
 152     @java.io.Serial
 153     private static final long serialVersionUID = 3078945930695997490L;
 154     /**
 155      * Constant for nanos per second.
 156      */
 157     private static final BigInteger BI_NANOS_PER_SECOND = BigInteger.valueOf(NANOS_PER_SECOND);
 158     /**
 159      * The pattern for parsing.
 160      */
 161     private static class Lazy {
< prev index next >