InstantSource Fixed, Offset, And Tick Clocks

Inject JDK 17 InstantSource instead of Clock when code only needs UTC instants, composing fixed, offset, and tick wrappers plus withZone adapters.

JavaConfig Management

Code

snippet
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.InstantSource;
import java.time.ZoneOffset;

public class TokenExpiryClock {

    static boolean isExpired(InstantSource source, Instant expiresAt) {
        return !source.instant().isBefore(expiresAt);
    }

    public static void main(String[] args) {
        Instant issuedAt = Instant.parse("2026-11-24T08:00:17Z");
        Instant expiresAt = Instant.parse("2026-11-24T08:05:00Z");

        // InstantSource drops ZoneId when domain logic works purely with UTC instants
        InstantSource fixed = InstantSource.fixed(issuedAt);
        System.out.println("fixed instant:  " + fixed.instant() + " millis=" + fixed.millis());
        System.out.println("expired at T+0: " + isExpired(fixed, expiresAt));

        // offset() shifts any base source by a fixed duration for deterministic tests
        InstantSource shifted = InstantSource.offset(fixed, Duration.ofMinutes(5));
        System.out.println("shifted T+5m:   " + shifted.instant() + " expired=" + isExpired(shifted, expiresAt));

        // tick() truncates readings to whole 10-second buckets for stable window keys
        InstantSource bucketed = InstantSource.tick(fixed, Duration.ofSeconds(10));
        System.out.println("10s tick read:  " + bucketed.instant());

        // Bridge to Clock only at edges that format calendar fields in a ZoneId
        Clock utcClock = fixed.withZone(ZoneOffset.UTC);
        System.out.println("bridged zone:   " + utcClock.getZone() + " -> " + utcClock.instant());
    }
}

Code flow overview — InstantSource Fixed, Offset, And Tick Clocks

The moving parts are the class TokenExpiryClock and the functions isExpired and main.

Points of care

  • No installation steps exist since standard platform modules ship all referenced APIs.

Applying it

The entry-point signature is static boolean isExpired(InstantSource source, Instant expiresAt)

The demo output is labeled (fixed instant:, expired at T+0:, shifted T+5m:); each label names the call that printed it.

main calls 1 helper in sequence: isExpired.

Statements that matter

return !source.instant().isBefore(expiresAt);

Interactive Tools for This Topic

FAQ

What does InstantSource Fixed, Offset, And Tick Clocks print when run?
Run it as a complete program — the output lines carry labels such as `fixed instant:`, `expired at T+0:`, `shifted T+5m:`.