Skip to content

Commit ec13c28

Browse files
committed
8366829: Add java.time.Duration constants MIN and MAX
Reviewed-by: rriggs, naoto, scolebourne
1 parent 430041d commit ec13c28

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,37 @@ public final class Duration
138138
* Constant for a duration of zero.
139139
*/
140140
public static final Duration ZERO = new Duration(0, 0);
141+
/**
142+
* The minimum supported {@code Duration}, which is {@link Long#MIN_VALUE}
143+
* seconds.
144+
*
145+
* @apiNote This constant represents the smallest possible instance of
146+
* {@code Duration}. Since {@code Duration} is directed, the smallest
147+
* possible duration is negative.
148+
*
149+
* The constant is intended to be used as a sentinel value or in tests.
150+
* Care should be taken when performing arithmetic on {@code MIN} as there
151+
* is a high risk that {@link ArithmeticException} or {@link DateTimeException}
152+
* will be thrown.
153+
*
154+
* @since 26
155+
*/
156+
public static final Duration MIN = new Duration(Long.MIN_VALUE, 0);
157+
/**
158+
* The maximum supported {@code Duration}, which is {@link Long#MAX_VALUE}
159+
* seconds and {@code 999,999,999} nanoseconds.
160+
*
161+
* @apiNote This constant represents the largest possible instance of
162+
* {@code Duration}.
163+
*
164+
* The constant is intended to be used as a sentinel value or in tests.
165+
* Care should be taken when performing arithmetic on {@code MAX} as there
166+
* is a high risk that {@link ArithmeticException} or {@link DateTimeException}
167+
* will be thrown.
168+
*
169+
* @since 26
170+
*/
171+
public static final Duration MAX = new Duration(Long.MAX_VALUE, 999_999_999);
141172
/**
142173
* Serialization version.
143174
*/

src/java.base/share/classes/java/time/temporal/ChronoUnit.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -184,10 +184,9 @@ public enum ChronoUnit implements TemporalUnit {
184184
* Artificial unit that represents the concept of forever.
185185
* This is primarily used with {@link TemporalField} to represent unbounded fields
186186
* such as the year or era.
187-
* The estimated duration of this unit is artificially defined as the largest duration
188-
* supported by {@link Duration}.
187+
* The estimated duration of this unit is artificially defined as {@link Duration#MAX}.
189188
*/
190-
FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999_999_999));
189+
FOREVER("Forever", Duration.MAX);
191190

192191
private final String name;
193192
private final Duration duration;

test/jdk/java/time/tck/java/time/TCKDuration.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -71,6 +71,8 @@
7171
import static java.time.temporal.ChronoUnit.WEEKS;
7272
import static java.time.temporal.ChronoUnit.YEARS;
7373
import static org.testng.Assert.assertEquals;
74+
import static org.testng.Assert.assertNotEquals;
75+
import static org.testng.Assert.assertThrows;
7476
import static org.testng.Assert.assertTrue;
7577
import static org.testng.Assert.fail;
7678

@@ -115,6 +117,44 @@ public void test_zero() {
115117
assertEquals(Duration.ZERO.getNano(), 0);
116118
}
117119

120+
@Test
121+
public void test_min() {
122+
assertEquals(Duration.MIN.getSeconds(), Long.MIN_VALUE);
123+
assertEquals(Duration.MIN.getNano(), 0);
124+
// no duration minimally less than MIN
125+
assertThrows(ArithmeticException.class, () -> Duration.MIN.minusNanos(1));
126+
}
127+
128+
@Test
129+
public void test_max() {
130+
assertEquals(Duration.MAX.getSeconds(), Long.MAX_VALUE);
131+
assertEquals(Duration.MAX.getNano(), 999_999_999);
132+
// no duration minimally greater than MAX
133+
assertThrows(ArithmeticException.class, () -> Duration.MAX.plusNanos(1));
134+
}
135+
136+
@Test
137+
public void test_constant_properties() {
138+
assertTrue(Duration.MIN.compareTo(Duration.MIN) == 0);
139+
assertEquals(Duration.MIN, Duration.MIN);
140+
assertTrue(Duration.ZERO.compareTo(Duration.ZERO) == 0);
141+
assertEquals(Duration.ZERO, Duration.ZERO);
142+
assertTrue(Duration.MAX.compareTo(Duration.MAX) == 0);
143+
assertEquals(Duration.MAX, Duration.MAX);
144+
145+
assertTrue(Duration.MIN.compareTo(Duration.ZERO) < 0);
146+
assertTrue(Duration.ZERO.compareTo(Duration.MIN) > 0);
147+
assertNotEquals(Duration.ZERO, Duration.MIN);
148+
149+
assertTrue(Duration.ZERO.compareTo(Duration.MAX) < 0);
150+
assertTrue(Duration.MAX.compareTo(Duration.ZERO) > 0);
151+
assertNotEquals(Duration.ZERO, Duration.MAX);
152+
153+
assertTrue(Duration.MIN.compareTo(Duration.MAX) < 0);
154+
assertTrue(Duration.MAX.compareTo(Duration.MIN) > 0);
155+
assertNotEquals(Duration.MIN, Duration.MAX);
156+
}
157+
118158
//-----------------------------------------------------------------------
119159
// ofSeconds(long)
120160
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)