CKTIS / Java
`List.size()` and `List.get(int)`: reading two valid offsets
Check how many elements a list contains before reading by index. When clocks go back and a local time occurs twice, read both valid UTC offsets explicitly.
Version: Java SE 21
Syntax
int size()
E get(int index)
int count = list.size();
E element = list.get(index); // 0 <= index && index < count, if the list has not changedOn java.util.List<E>, List.size() returns the number of elements as an int. If the list contains more than Integer.MAX_VALUE elements, it returns Integer.MAX_VALUE. List.get(int) returns the element of type E at the given position. Indexing starts at zero: get(0) reads the first element, not the element count.
An index is valid when 0 <= index && index < list.size(). A negative index, or one equal to or greater than size(), causes IndexOutOfBoundsException. An empty list has no valid index. Checking the size first does not guarantee that an index remains valid if the list changes before the read.
ZoneRules.getValidOffsets(LocalDateTime) returns a List<ZoneOffset> with 0 elements in a gap, 1 in the normal case, and typically 2 in an overlap, when local time repeats. The API allows for more than two in theory. When offsets.size() == 2, offsets.get(0) gives the offset for the earlier occurrence of the same local date and time, and offsets.get(1) gives the offset for the later occurrence. The order is chronological, not ascending by numeric offset.
Examples
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.zone.ZoneOffsetTransition;
import java.time.zone.ZoneRules;
import java.util.List;
public class Main {
public static void main(String[] args) {
ZoneOffset before = ZoneOffset.ofHours(2);
ZoneOffset after = ZoneOffset.ofHours(1);
LocalDateTime cutoff = LocalDateTime.of(2024, 10, 27, 3, 0);
ZoneOffsetTransition rollback = ZoneOffsetTransition.of(cutoff, before, after);
ZoneRules rules = ZoneRules.of(after, before, List.of(), List.of(rollback), List.of());
LocalDateTime local = LocalDateTime.of(2024, 10, 27, 2, 30);
List<ZoneOffset> offsets = rules.getValidOffsets(local);
int count = offsets.size();
if (count != 2) {
throw new IllegalStateException("Expected exactly two valid offsets");
}
ZoneOffset earlier = offsets.get(0);
ZoneOffset later = offsets.get(1);
System.out.println(count);
System.out.println(earlier.getId());
System.out.println(later.getId());
}
}Result
2 +02:00 +01:00
The example defines its own clock rollback from 03:00 to 02:00. It does not consult an installed time-zone database. After checking that the list contains exactly two elements, it prints the count followed by `+02:00` and `+01:00`.
Related lessons
- Java · Lesson 60 · Advanced · Version: 2
Sources
- List (Java SE 21 & JDK 21) — get(int)
- List (Java SE 21 & JDK 21) — size()
- ZoneRules (Java SE 21 & JDK 21) — getValidOffsets(LocalDateTime)
- ZoneRules (Java SE 21 & JDK 21) — of(ZoneOffset, ZoneOffset, List, List, List)
- ZoneOffsetTransition (Java SE 21 & JDK 21) — of(LocalDateTime, ZoneOffset, ZoneOffset)
- ZoneOffset (Java SE 21 & JDK 21) — getId()