Java : Clock con ejemplos
Clock (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de Clock.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
System.out.println(ZoneId.systemDefault()); // America/Los_Angeles
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-06T23:04:58.859384800-07:00[America/Los_Angeles]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-06T23:04:58.861384100-07:00[America/Los_Angeles]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.861384100Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:04:58.864382800Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.865382100Z
}
{
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
Constructors
Clock ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
boolean equals (Object obj)
final var clock1 = Clock.systemUTC();
final var clock2 = Clock.systemUTC();
final var clock3 = Clock.systemDefaultZone();
System.out.println(clock1.equals(clock2)); // true
System.out.println(clock1.equals(clock3)); // false
static Clock fixed (Instant fixedInstant, ZoneId zone)
System.out.println(ZoneId.systemDefault()); // America/Los_Angeles
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-06T23:04:58.859384800-07:00[America/Los_Angeles]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-06T23:04:58.861384100-07:00[America/Los_Angeles]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.861384100Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:04:58.864382800Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.865382100Z
}
{
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
abstract ZoneId getZone ()
System.out.println(ZoneId.systemDefault()); // America/Los_Angeles
final var clock1 = Clock.systemDefaultZone();
System.out.println(clock1.getZone()); // America/Los_Angeles
final var clock2 = Clock.systemUTC();
System.out.println(clock2.getZone()); // Z
final var zone = ZoneId.of("Asia/Tokyo");
final var clock3 = Clock.system(zone);
System.out.println(clock3.getZone()); // Asia/Tokyo
int hashCode ()
final var clock1 = Clock.systemUTC();
System.out.println(clock1.hashCode()); // 1
final var clock2 = Clock.system(ZoneId.of("America/Los_Angeles"));
System.out.println(clock2.hashCode()); // -1536188512
final var clock3 = Clock.system(ZoneId.of("Asia/Tokyo"));
System.out.println(clock3.hashCode()); // -1660747038
abstract Instant instant ()
final var clock = Clock.systemUTC();
for (int i = 0; i < 4; i++) {
final var instant = clock.instant();
final var millis = clock.millis();
System.out.printf("%s (millis = %d)%n", instant, millis);
TimeUnit.SECONDS.sleep(10);
}
// Result
// ↓
//2024-07-07T06:10:53.794095300Z (millis = 1720332653794)
//2024-07-07T06:11:03.811190700Z (millis = 1720332663811)
//2024-07-07T06:11:13.827016100Z (millis = 1720332673827)
//2024-07-07T06:11:23.841881800Z (millis = 1720332683841)
long millis ()
final var clock = Clock.systemUTC();
for (int i = 0; i < 4; i++) {
final var instant = clock.instant();
final var millis = clock.millis();
System.out.printf("%s (millis = %d)%n", instant, millis);
TimeUnit.SECONDS.sleep(10);
}
// Result
// ↓
//2024-07-07T06:10:53.794095300Z (millis = 1720332653794)
//2024-07-07T06:11:03.811190700Z (millis = 1720332663811)
//2024-07-07T06:11:13.827016100Z (millis = 1720332673827)
//2024-07-07T06:11:23.841881800Z (millis = 1720332683841)
static Clock offset (Clock baseClock, Duration offsetDuration)
final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z
final var baseClock = Clock.fixed(instant, ZoneOffset.UTC);
System.out.println(baseClock.instant()); // 2100-01-01T00:00:00Z
final var clock1 = Clock.offset(baseClock, Duration.ofDays(-1));
System.out.println(clock1.instant()); // 2099-12-31T00:00:00Z
final var clock2 = Clock.offset(baseClock, Duration.ofHours(14).plusMinutes(30));
System.out.println(clock2.instant()); // 2100-01-01T14:30:00Z
static Clock system (ZoneId zone)
System.out.println(ZoneId.systemDefault()); // America/Los_Angeles
final var clock1 = Clock.systemDefaultZone();
System.out.println(clock1.getZone()); // America/Los_Angeles
final var clock2 = Clock.systemUTC();
System.out.println(clock2.getZone()); // Z
final var zone = ZoneId.of("Asia/Tokyo");
final var clock3 = Clock.system(zone);
System.out.println(clock3.getZone()); // Asia/Tokyo
static Clock systemDefaultZone ()
System.out.println(ZoneId.systemDefault()); // America/Los_Angeles
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-06T23:04:58.859384800-07:00[America/Los_Angeles]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-06T23:04:58.861384100-07:00[America/Los_Angeles]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.861384100Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:04:58.864382800Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.865382100Z
}
{
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
static Clock systemUTC ()
System.out.println(ZoneId.systemDefault()); // America/Los_Angeles
{
final var now = ZonedDateTime.now();
System.out.println(now); // 2024-07-06T23:04:58.859384800-07:00[America/Los_Angeles]
}
{
final var clock = Clock.systemDefaultZone();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-06T23:04:58.861384100-07:00[America/Los_Angeles]
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.861384100Z
}
{
final var clock = Clock.systemUTC();
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2024-07-07T06:04:58.864382800Z
final var instant = clock.instant();
System.out.println(instant); // 2024-07-07T06:04:58.865382100Z
}
{
final var dateTime = ZonedDateTime.of(2100, 12, 31, 0, 0, 0, 0, ZoneOffset.UTC);
final var clock = Clock.fixed(dateTime.toInstant(), dateTime.getZone());
final var now = ZonedDateTime.now(clock);
System.out.println(now); // 2100-12-31T00:00Z
final var instant = clock.instant();
System.out.println(instant); // 2100-12-31T00:00:00Z
}
static Clock tick (Clock baseClock, Duration tickDuration)
final var baseClock = Clock.systemUTC();
final var clock = Clock.tick(baseClock, Duration.ofSeconds(2));
for (int i = 0; i < 8; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.SECONDS.sleep(1);
}
// Result
// ↓
//2024-07-07T06:13:14Z
//2024-07-07T06:13:16Z
//2024-07-07T06:13:16Z
//2024-07-07T06:13:18Z
//2024-07-07T06:13:18Z
//2024-07-07T06:13:20Z
//2024-07-07T06:13:20Z
//2024-07-07T06:13:22Z
static Clock tickMillis (ZoneId zone)
final var clock = Clock.tickMillis(ZoneOffset.UTC);
for (int i = 0; i < 8; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.MILLISECONDS.sleep(1);
}
// Result
// ↓
//2024-07-07T06:13:40.483Z
//2024-07-07T06:13:40.489Z
//2024-07-07T06:13:40.491Z
//2024-07-07T06:13:40.493Z
//2024-07-07T06:13:40.495Z
//2024-07-07T06:13:40.497Z
//2024-07-07T06:13:40.499Z
//2024-07-07T06:13:40.502Z
static Clock tickMinutes (ZoneId zone)
final var clock = Clock.tickMinutes(ZoneOffset.UTC);
for (int i = 0; i < 4; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.SECONDS.sleep(30);
}
// Result
// ↓
//2024-07-07T06:15:00Z
//2024-07-07T06:15:00Z
//2024-07-07T06:16:00Z
//2024-07-07T06:16:00Z
static Clock tickSeconds (ZoneId zone)
final var clock = Clock.tickSeconds(ZoneOffset.UTC);
for (int i = 0; i < 8; i++) {
final var instant = clock.instant();
System.out.println(instant);
TimeUnit.MILLISECONDS.sleep(500);
}
// Result
// ↓
//2024-07-07T06:18:04Z
//2024-07-07T06:18:05Z
//2024-07-07T06:18:05Z
//2024-07-07T06:18:06Z
//2024-07-07T06:18:06Z
//2024-07-07T06:18:07Z
//2024-07-07T06:18:07Z
//2024-07-07T06:18:08Z
abstract Clock withZone (ZoneId zone)
final var clock1 = Clock.systemUTC();
System.out.println(clock1); // SystemClock[Z]
final var zone = ZoneId.of("America/Los_Angeles");
final var clock2 = clock1.withZone(zone);
System.out.println(clock2); // SystemClock[America/Los_Angeles]
final var clock1 = Clock.tickSeconds(ZoneOffset.UTC);
System.out.println(clock1); // TickClock[SystemClock[Z],PT1S]
final var zone = ZoneId.of("Asia/Tokyo");
final var clock2 = clock1.withZone(zone);
System.out.println(clock2); // TickClock[SystemClock[Asia/Tokyo],PT1S]
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset