Java : InstantSource with Examples

InstantSource (Java SE 20 & JDK 20) with Examples.
You will find code examples on most InstantSource methods.


Summary

Provides access to the current instant.

Class diagram

final var now = Instant.now();
System.out.println(now); // 2023-10-11T07:00:59.708305600Z
final var source = InstantSource.system();

final var instant = source.instant();
System.out.println(instant); // 2023-10-11T07:00:59.716300700Z
final var dateTime = ZonedDateTime.of(1999, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var source = InstantSource.fixed(dateTime.toInstant());

final var instant = source.instant();
System.out.println(instant); // 1999-12-31T00:00:00Z

Methods

static InstantSource fixed (Instant fixedInstant)

Obtains a source that always returns the same instant.

final var now = Instant.now();
System.out.println(now); // 2023-10-11T07:00:59.708305600Z
final var source = InstantSource.system();

final var instant = source.instant();
System.out.println(instant); // 2023-10-11T07:00:59.716300700Z
final var dateTime = ZonedDateTime.of(1999, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var source = InstantSource.fixed(dateTime.toInstant());

final var instant = source.instant();
System.out.println(instant); // 1999-12-31T00:00:00Z

Instant instant ()

Gets the current instant of the source.

final var source = InstantSource.system();

for (int i = 0; i < 4; i++) {
    final var instant = source.instant();
    final var millis = source.millis();

    System.out.printf("%s (millis = %d)%n", instant, millis);

    TimeUnit.SECONDS.sleep(10);
}

// Result
// ↓
//2023-10-11T07:02:30.858307300Z (millis = 1697007750858)
//2023-10-11T07:02:40.867718300Z (millis = 1697007760867)
//2023-10-11T07:02:50.878152500Z (millis = 1697007770878)
//2023-10-11T07:03:00.888919200Z (millis = 1697007780888)

default long millis ()

Gets the current millisecond instant of the source.

Please see instant().

static InstantSource offset (InstantSource baseSource, Duration offsetDuration)

Obtains a source that returns instants from the specified source with the specified duration added.

final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

final var baseSource = InstantSource.fixed(instant);
System.out.println(baseSource.instant()); // 2100-01-01T00:00:00Z

final var source1 = InstantSource.offset(baseSource, Duration.ofDays(-1));
System.out.println(source1.instant()); // 2099-12-31T00:00:00Z

final var source2 = InstantSource.offset(baseSource, Duration.ofHours(14).plusMinutes(30));
System.out.println(source2.instant()); // 2100-01-01T14:30:00Z

static InstantSource system ()

Obtains a source that returns the current instant using the best available system clock.

Please see fixed(Instant fixedInstant).

static InstantSource tick (InstantSource baseSource, Duration tickDuration)

Obtains a source that returns instants from the specified source truncated to the nearest occurrence of the specified duration.

final var baseSource = InstantSource.system();
final var souce = InstantSource.tick(baseSource, Duration.ofSeconds(2));

for (int i = 0; i < 8; i++) {
    final var instant = souce.instant();
    System.out.println(instant);

    TimeUnit.SECONDS.sleep(1);
}

// Result
// ↓
//2023-10-11T07:07:00Z
//2023-10-11T07:07:00Z
//2023-10-11T07:07:02Z
//2023-10-11T07:07:02Z
//2023-10-11T07:07:04Z
//2023-10-11T07:07:04Z
//2023-10-11T07:07:06Z
//2023-10-11T07:07:06Z

default Clock withZone (ZoneId zone)

Returns a clock with the specified time-zone.

final var source = InstantSource.system();
System.out.println(source); // SystemInstantSource

{
    final var zone = ZoneId.of("America/Los_Angeles");
    final var clock = source.withZone(zone);
    System.out.println(clock); // SystemClock[America/Los_Angeles]
}
{
    final var zone = ZoneId.of("Asia/Tokyo");
    final var clock = source.withZone(zone);
    System.out.println(clock); // SystemClock[Asia/Tokyo]
}

Related posts

To top of page