Java SE API
Clock.fixed()
Create a clock that always returns the specified instant. Java SE API; example uses Java 21-compatible syntax.
Formal Syntax & Structure
java syntaxjava
static Clock fixed(Instant fixedInstant, ZoneId zone)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fixedInstant | Instant | Fixed timeline position |
| zone | ZoneId | Clock zone |
Return Value
Clock
Create a clock that always returns the specified instant.
Working Example
Example Snippetjava
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main { public static void main(String[] args) throws Exception { Clock clock = Clock.fixed(Instant.parse("2024-01-01T23:30:00Z"), ZoneId.of("Europe/Stockholm")); System.out.println(LocalDate.now(clock)); System.out.println(Instant.now(clock)); Date legacy = Date.from(Instant.now(clock)); System.out.println(legacy.toInstant().equals(Instant.now(clock))); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT); cal.setTime(legacy); System.out.println(cal.toInstant()); }}