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.
Code
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
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.
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:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.