Java : OffsetTime con ejemplos
OffsetTime (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de OffsetTime.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var time = OffsetTime.of(13, 30, 15, 123000000, ZoneOffset.ofHours(4));
System.out.println(time); // 13:30:15.123+04:00
System.out.println(time.getHour()); // 13
System.out.println(time.getMinute()); // 30
System.out.println(time.getSecond()); // 15
System.out.println(time.getNano()); // 123000000
System.out.println(time.getOffset()); // +04:00
Fields
static final OffsetTime MAX
System.out.println(OffsetTime.MAX); // 23:59:59.999999999-18:00
System.out.println(OffsetTime.MAX.getHour()); // 23
System.out.println(OffsetTime.MAX.getMinute()); // 59
System.out.println(OffsetTime.MAX.getSecond()); // 59
System.out.println(OffsetTime.MAX.getNano()); // 999999999
System.out.println(OffsetTime.MAX.getOffset()); // -18:00
static final OffsetTime MIN
System.out.println(OffsetTime.MIN); // 00:00+18:00
System.out.println(OffsetTime.MIN.getHour()); // 0
System.out.println(OffsetTime.MIN.getMinute()); // 0
System.out.println(OffsetTime.MIN.getSecond()); // 0
System.out.println(OffsetTime.MIN.getNano()); // 0
System.out.println(OffsetTime.MIN.getOffset()); // +18:00
Methods
Temporal adjustInto (Temporal temporal)
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
final var temporal = OffsetDateTime.of(2100, 1, 1, 2, 3, 0, 0, offset);
System.out.println(temporal); // 2100-01-01T02:03Z
final var time = OffsetTime.of(23, 59, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 23:59+04:00
final var ret = time.adjustInto(temporal);
System.out.println(ret); // 2100-01-01T23:59+04:00
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var temporal = ZonedDateTime.of(2100, 1, 1, 2, 3, 0, 0, zone);
System.out.println(temporal); // 2100-01-01T02:03-01:00[Etc/GMT+1]
final var time = OffsetTime.of(20, 30, 40, 123456, ZoneOffset.UTC);
System.out.println(time); // 20:30:40.000123456Z
final var ret = time.adjustInto(temporal);
System.out.println(ret); // 2100-01-01T20:30:40.000123456-01:00[Etc/GMT+1]
OffsetDateTime atDate (LocalDate date)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
final var ret1 = time.atDate(LocalDate.of(2100, 4, 12));
System.out.println(ret1); // 2100-04-12T13:30:15Z
final var ret2 = time.atDate(LocalDate.of(-900, 1, 1));
System.out.println(ret2); // -0900-01-01T13:30:15Z
int compareTo (OffsetTime other)
final var time1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time1); // 00:00+04:00
final var time2 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(-8));
System.out.println(time2); // 00:00-08:00
System.out.println(time1.compareTo(time2)); // -1
final var time1 = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:30:15Z
final var time2 = OffsetTime.of(14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time2); // 14:30:15Z
System.out.println(time1.compareTo(time2)); // -1
final var time1 = OffsetTime.of(13, 31, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:31Z
final var time2 = OffsetTime.of(13, 30, 59, 0, ZoneOffset.UTC);
System.out.println(time2); // 13:30:59Z
System.out.println(time1.compareTo(time2)); // 1
final var time1 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 23:30Z
final var time2 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 23:30Z
System.out.println(time1.compareTo(time2)); // 0
boolean equals (Object obj)
final var time1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time1); // 00:00+04:00
final var time2 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(-8));
System.out.println(time2); // 00:00-08:00
System.out.println(time1.equals(time2)); // false
final var time1 = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:30:15Z
final var time2 = OffsetTime.of(14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time2); // 14:30:15Z
System.out.println(time1.equals(time2)); // false
final var time1 = OffsetTime.of(13, 31, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:31Z
final var time2 = OffsetTime.of(13, 30, 59, 0, ZoneOffset.UTC);
System.out.println(time2); // 13:30:59Z
System.out.println(time1.equals(time2)); // false
final var time1 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 23:30Z
final var time2 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 23:30Z
System.out.println(time1.equals(time2)); // true
String format (DateTimeFormatter formatter)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 13:30:15+04:00
{
final var ret1 = time.format(DateTimeFormatter.ISO_OFFSET_TIME);
System.out.println(ret1); // 13:30:15+04:00
final var ret2 = time.format(DateTimeFormatter.ofPattern("HH:mm:ss O"));
System.out.println(ret2); // 13:30:15 GMT+4
}
{
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var ret1 = time.format(
DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
System.out.println(ret1); // 1:30:15 PM
final var ret2 = time.format(
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
System.out.println(ret2); // 1:30 PM
try {
final var ret3 = time.format(
DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG));
} catch (DateTimeException e) {
System.out.println("DateTimeException! : " + e.getMessage());
}
// Result
// ↓
//DateTimeException! :
// Unable to extract ZoneId from temporal 13:30:15+04:00 with chronology ISO
}
static OffsetTime from (TemporalAccessor temporal)
final var offset = ZoneOffset.ofHours(12);
System.out.println(offset); // +12:00
final var temporal = OffsetDateTime.of(2100, 12, 15, 4, 5, 6, 123000, offset);
System.out.println(temporal); // 2100-12-15T04:05:06.000123+12:00
System.out.println(OffsetTime.from(temporal)); // 04:05:06.000123+12:00
final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1
final var temporal = ZonedDateTime.of(1800, 1, 3, 1, 2, 3, 456, zone);
System.out.println(temporal); // 1800-01-03T01:02:03.000000456-01:00[Etc/GMT+1]
System.out.println(OffsetTime.from(temporal)); // 01:02:03.000000456-01:00
int get (TemporalField field)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.ofHours(1));
System.out.println(time); // 13:30:15.000000123+01:00
System.out.println(time.get(ChronoField.HOUR_OF_DAY)); // 13
System.out.println(time.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(time.get(ChronoField.SECOND_OF_MINUTE)); // 15
System.out.println(time.get(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(time.get(ChronoField.OFFSET_SECONDS)); // 3600
int getHour ()
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getHour()); // 13
long getLong (TemporalField field)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.ofHours(1));
System.out.println(time); // 13:30:15.000000123+01:00
System.out.println(time.getLong(ChronoField.HOUR_OF_DAY)); // 13
System.out.println(time.getLong(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(time.getLong(ChronoField.SECOND_OF_MINUTE)); // 15
System.out.println(time.getLong(ChronoField.NANO_OF_SECOND)); // 123
System.out.println(time.getLong(ChronoField.OFFSET_SECONDS)); // 3600
int getMinute ()
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getMinute()); // 30
int getNano ()
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getNano()); // 0
final var time = OffsetTime.of(13, 30, 15, 123456789, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.123456789Z
System.out.println(time.getNano()); // 123456789
ZoneOffset getOffset ()
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getOffset()); // Z
final var time = OffsetTime.of(1, 2, 3, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 01:02:03+04:00
System.out.println(time.getOffset()); // +04:00
int getSecond ()
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.getSecond()); // 15
int hashCode ()
final var time1 = OffsetTime.of(1, 2, 3, 4, ZoneOffset.UTC);
System.out.println(time1.hashCode()); // -736645786
final var time2 = OffsetTime.of(1, 2, 3, 4, ZoneOffset.ofHours(4));
System.out.println(time2.hashCode()); // -736651994
final var time3 = OffsetTime.of(23, 59, 59, 0, ZoneOffset.UTC);
System.out.println(time3.hashCode()); // 1437890708
boolean isAfter (OffsetTime other)
final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:01Z
final var time2 = OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time4 = OffsetTime.of(12, 2, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 12:00Z
System.out.println(time3); // 12:01Z
System.out.println(time4); // 12:02Z
System.out.println(time.isAfter(time2)); // true
System.out.println(time.isAfter(time3)); // false
System.out.println(time.isAfter(time4)); // false
final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:01+04:00
final var time2 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(-4));
final var time4 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(8));
System.out.println(time2); // 12:01Z
System.out.println(time3); // 12:01-04:00
System.out.println(time4); // 12:01+08:00
System.out.println(time.isAfter(time2)); // false
System.out.println(time.isAfter(time3)); // false
System.out.println(time.isAfter(time4)); // true
boolean isBefore (OffsetTime other)
final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:01Z
final var time2 = OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time4 = OffsetTime.of(12, 2, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 12:00Z
System.out.println(time3); // 12:01Z
System.out.println(time4); // 12:02Z
System.out.println(time.isBefore(time2)); // false
System.out.println(time.isBefore(time3)); // false
System.out.println(time.isBefore(time4)); // true
final var time = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:01+04:00
final var time2 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.UTC);
final var time3 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(-4));
final var time4 = OffsetTime.of(12, 1, 0, 0, ZoneOffset.ofHours(8));
System.out.println(time2); // 12:01Z
System.out.println(time3); // 12:01-04:00
System.out.println(time4); // 12:01+08:00
System.out.println(time.isBefore(time2)); // true
System.out.println(time.isBefore(time3)); // true
System.out.println(time.isBefore(time4)); // false
boolean isEqual (OffsetTime other)
final var time1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time1); // 00:00+04:00
final var time2 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.ofHours(-8));
System.out.println(time2); // 00:00-08:00
System.out.println(time1.isEqual(time2)); // false
final var time1 = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:30:15Z
final var time2 = OffsetTime.of(14, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time2); // 14:30:15Z
System.out.println(time1.isEqual(time2)); // false
final var time1 = OffsetTime.of(13, 31, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 13:31Z
final var time2 = OffsetTime.of(13, 30, 59, 0, ZoneOffset.UTC);
System.out.println(time2); // 13:30:59Z
System.out.println(time1.isEqual(time2)); // false
final var time1 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time1); // 23:30Z
final var time2 = OffsetTime.of(23, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time2); // 23:30Z
System.out.println(time1.isEqual(time2)); // true
boolean isSupported (TemporalField field)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(time.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(time.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(time.isSupported(ChronoField.NANO_OF_SECOND)); // true
System.out.println(time.isSupported(ChronoField.OFFSET_SECONDS)); // true
System.out.println(time.isSupported(ChronoField.YEAR)); // false
boolean isSupported (TemporalUnit unit)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time.isSupported(ChronoUnit.HOURS)); // true
System.out.println(time.isSupported(ChronoUnit.MINUTES)); // true
System.out.println(time.isSupported(ChronoUnit.SECONDS)); // true
System.out.println(time.isSupported(ChronoUnit.NANOS)); // true
System.out.println(time.isSupported(ChronoUnit.DAYS)); // false
OffsetTime minus (long amountToSubtract, TemporalUnit unit)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z
System.out.println(time.minus(1, ChronoUnit.HOURS)); // 12:30:15.000000123Z
System.out.println(time.minus(2, ChronoUnit.MINUTES)); // 13:28:15.000000123Z
System.out.println(time.minus(3, ChronoUnit.SECONDS)); // 13:30:12.000000123Z
System.out.println(time.minus(4, ChronoUnit.NANOS)); // 13:30:15.000000119Z
OffsetTime minus (TemporalAmount amountToSubtract)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z
System.out.println(time.minus(Duration.ofHours(1))); // 12:30:15.000000123Z
System.out.println(time.minus(Duration.ofMinutes(2))); // 13:28:15.000000123Z
System.out.println(time.minus(Duration.ofSeconds(3))); // 13:30:12.000000123Z
System.out.println(time.minus(Duration.ofNanos(4))); // 13:30:15.000000119Z
OffsetTime minusHours (long hours)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.minusHours(1)); // 12:30:15Z
System.out.println(time.minusHours(2)); // 11:30:15Z
System.out.println(time.minusHours(13)); // 00:30:15Z
System.out.println(time.minusHours(14)); // 23:30:15Z
OffsetTime minusMinutes (long minutes)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.minusMinutes(1)); // 13:29:15Z
System.out.println(time.minusMinutes(2)); // 13:28:15Z
System.out.println(time.minusMinutes(30)); // 13:00:15Z
System.out.println(time.minusMinutes(31)); // 12:59:15Z
System.out.println(time.minusMinutes(60 * 13)); // 00:30:15Z
System.out.println(time.minusMinutes(60 * 14)); // 23:30:15Z
OffsetTime minusNanos (long nanos)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z
System.out.println(time.minusNanos(1L)); // 13:30:15.000000122Z
System.out.println(time.minusNanos(2L)); // 13:30:15.000000121Z
System.out.println(time.minusNanos(123L)); // 13:30:15Z
System.out.println(time.minusNanos(124L)); // 13:30:14.999999999Z
OffsetTime minusSeconds (long seconds)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.minusSeconds(1)); // 13:30:14Z
System.out.println(time.minusSeconds(2)); // 13:30:13Z
System.out.println(time.minusSeconds(15)); // 13:30Z
System.out.println(time.minusSeconds(16)); // 13:29:59Z
System.out.println(time.minusSeconds(76)); // 13:28:59Z
System.out.println(time.minusSeconds(60 * 30)); // 13:00:15Z
System.out.println(time.minusSeconds(60 * 31)); // 12:59:15Z
static OffsetTime now ()
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
System.out.println(OffsetTime.now()); // 01:05:07.322175800-07:00
static OffsetTime now (Clock clock)
// A clock advanced 5 hours.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofHours(5));
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var time1 = OffsetTime.now();
System.out.println(time1); // 01:02:46.655392100-07:00
final var time2 = OffsetTime.now(clock);
System.out.println(time2); // 06:02:46.656392400-07:00
static OffsetTime now (ZoneId zone)
final var zone1 = ZoneOffset.UTC;
System.out.println(zone1); // Z
final var zone2 = ZoneId.of("Etc/GMT+1");
System.out.println(zone2); // Etc/GMT+1
final var time1 = OffsetTime.now(zone1);
System.out.println(time1); // 08:06:37.659861400Z
final var time2 = OffsetTime.now(zone2);
System.out.println(time2); // 07:06:37.660860500-01:00
static OffsetTime of (int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)
final var ret1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(ret1); // 00:00Z
final var ret2 = OffsetTime.of(12, 30, 45, 1234, ZoneOffset.ofHours(4));
System.out.println(ret2); // 12:30:45.000001234+04:00
static OffsetTime of (LocalTime time, ZoneOffset offset)
final var localTime = LocalTime.of(0, 0);
System.out.println(localTime); // 00:00
final var ret = OffsetTime.of(localTime, ZoneOffset.UTC);
System.out.println(ret); // 00:00Z
final var localTime = LocalTime.of(12, 30, 45, 123);
System.out.println(localTime); // 12:30:45.000000123
final var ret = OffsetTime.of(localTime, ZoneOffset.ofHours(4));
System.out.println(ret); // 12:30:45.000000123+04:00
static OffsetTime ofInstant (Instant instant, ZoneId zone)
final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z
final var ret1 = OffsetTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(ret1); // 00:00Z
final var ret2 = OffsetTime.ofInstant(instant, ZoneOffset.ofHours(8));
System.out.println(ret2); // 08:00+08:00
final var ret3 = OffsetTime.ofInstant(instant, ZoneOffset.ofHours(-8));
System.out.println(ret3); // 16:00-08:00
static OffsetTime parse (CharSequence text)
System.out.println(OffsetTime.parse("12:30Z")); // 12:30Z
System.out.println(OffsetTime.parse("23:59:59.123456789Z")); // 23:59:59.123456789Z
System.out.println(OffsetTime.parse("02:05:01+04:00")); // 02:05:01+04:00
static OffsetTime parse (CharSequence text, DateTimeFormatter formatter)
final var ret1 = OffsetTime.parse(
"12:30:15+04:00", DateTimeFormatter.ISO_OFFSET_TIME);
System.out.println(ret1); // 12:30:15+04:00
final var ret2 = OffsetTime.parse(
"2:5:1 Z", DateTimeFormatter.ofPattern("H:m:s X"));
System.out.println(ret2); // 02:05:01Z
final var ret3 = OffsetTime.parse(
"12:30:15 GMT-8", DateTimeFormatter.ofPattern("H:m:s O"));
System.out.println(ret3); // 12:30:15-08:00
OffsetTime plus (long amountToAdd, TemporalUnit unit)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z
System.out.println(time.plus(1, ChronoUnit.HOURS)); // 14:30:15.000000123Z
System.out.println(time.plus(2, ChronoUnit.MINUTES)); // 13:32:15.000000123Z
System.out.println(time.plus(3, ChronoUnit.SECONDS)); // 13:30:18.000000123Z
System.out.println(time.plus(4, ChronoUnit.NANOS)); // 13:30:15.000000127Z
OffsetTime plus (TemporalAmount amountToAdd)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z
System.out.println(time.plus(Duration.ofHours(1))); // 14:30:15.000000123Z
System.out.println(time.plus(Duration.ofMinutes(2))); // 13:32:15.000000123Z
System.out.println(time.plus(Duration.ofSeconds(3))); // 13:30:18.000000123Z
System.out.println(time.plus(Duration.ofNanos(4))); // 13:30:15.000000127Z
OffsetTime plusHours (long hours)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.plusHours(1)); // 14:30:15Z
System.out.println(time.plusHours(2)); // 15:30:15Z
System.out.println(time.plusHours(10)); // 23:30:15Z
System.out.println(time.plusHours(11)); // 00:30:15Z
OffsetTime plusMinutes (long minutes)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.plusMinutes(1)); // 13:31:15Z
System.out.println(time.plusMinutes(2)); // 13:32:15Z
System.out.println(time.plusMinutes(29)); // 13:59:15Z
System.out.println(time.plusMinutes(30)); // 14:00:15Z
System.out.println(time.plusMinutes(31)); // 14:01:15Z
System.out.println(time.plusMinutes(60 * 10)); // 23:30:15Z
System.out.println(time.plusMinutes(60 * 11)); // 00:30:15Z
OffsetTime plusNanos (long nanos)
final var time = OffsetTime.of(13, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 13:30:15.000000123Z
System.out.println(time.plusNanos(1L)); // 13:30:15.000000124Z
System.out.println(time.plusNanos(2L)); // 13:30:15.000000125Z
System.out.println(time.plusNanos(999999999L - 123)); // 13:30:15.999999999Z
System.out.println(time.plusNanos(999999999L - 122)); // 13:30:16Z
OffsetTime plusSeconds (long seconds)
final var time = OffsetTime.of(13, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 13:30:15Z
System.out.println(time.plusSeconds(1)); // 13:30:16Z
System.out.println(time.plusSeconds(2)); // 13:30:17Z
System.out.println(time.plusSeconds(44)); // 13:30:59Z
System.out.println(time.plusSeconds(45)); // 13:31Z
System.out.println(time.plusSeconds(46)); // 13:31:01Z
System.out.println(time.plusSeconds(60 * 29)); // 13:59:15Z
System.out.println(time.plusSeconds(60 * 30)); // 14:00:15Z
<R> R query (TemporalQuery<R> query)
final var time = OffsetTime.of(12, 30, 20, 123456789, ZoneOffset.ofHours(4));
System.out.println(time); // 12:30:20.123456789+04:00
System.out.println(time.query(TemporalQueries.localTime())); // 12:30:20.123456789
System.out.println(time.query(TemporalQueries.offset())); // +04:00
System.out.println(time.query(TemporalQueries.precision())); // Nanos
ValueRange range (TemporalField field)
final var time = OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(time.range(ChronoField.HOUR_OF_DAY)); // 0 - 23
System.out.println(time.range(ChronoField.MINUTE_OF_HOUR)); // 0 - 59
System.out.println(time.range(ChronoField.SECOND_OF_MINUTE)); // 0 - 59
System.out.println(time.range(ChronoField.NANO_OF_SECOND)); // 0 - 999999999
System.out.println(time.range(ChronoField.OFFSET_SECONDS)); // -64800 - 64800
long toEpochSecond (LocalDate date)
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z
final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08
final long ret = time.toEpochSecond(date);
System.out.println(ret); // 4110870600
System.out.println(Instant.ofEpochSecond(ret)); // 2100-04-08T12:30:00Z
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.ofHours(8));
System.out.println(time); // 12:30+08:00
final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08
final long ret = time.toEpochSecond(date);
System.out.println(ret); // 4110841800
System.out.println(Instant.ofEpochSecond(ret)); // 2100-04-08T04:30:00Z
LocalTime toLocalTime ()
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z
final var ret = time.toLocalTime();
System.out.println(ret); // 12:30
final var time = OffsetTime.of(1, 2, 3, 456, ZoneOffset.ofHours(8));
System.out.println(time); // 01:02:03.000000456+08:00
final var ret = time.toLocalTime();
System.out.println(ret); // 01:02:03.000000456
String toString ()
final var str1 = OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC).toString();
System.out.println(str1); // 00:00Z
final var str2 = OffsetTime.of(1, 2, 3, 456, ZoneOffset.ofHours(4)).toString();
System.out.println(str2); // 01:02:03.000000456+04:00
OffsetTime truncatedTo (TemporalUnit unit)
final var time = OffsetTime.of(12, 30, 15, 123456789, ZoneOffset.UTC);
System.out.println(time); // 12:30:15.123456789Z
System.out.println(time.truncatedTo(ChronoUnit.HOURS)); // 12:00Z
System.out.println(time.truncatedTo(ChronoUnit.MINUTES)); // 12:30Z
System.out.println(time.truncatedTo(ChronoUnit.SECONDS)); // 12:30:15Z
System.out.println(time.truncatedTo(ChronoUnit.NANOS)); // 12:30:15.123456789Z
long until (Temporal endExclusive, TemporalUnit unit)
final var time1 = OffsetTime.of(1, 2, 0, 0, ZoneOffset.UTC);
final var time2 = OffsetTime.of(3, 32, 15, 123, ZoneOffset.UTC);
System.out.println(time1); // 01:02Z
System.out.println(time2); // 03:32:15.000000123Z
System.out.println(time1.until(time2, ChronoUnit.HOURS)); // 2
System.out.println(time1.until(time2, ChronoUnit.MINUTES)); // 150
System.out.println(time1.until(time2, ChronoUnit.SECONDS)); // 9015
System.out.println(time1.until(time2, ChronoUnit.NANOS)); // 9015000000123
final var time1 = OffsetTime.of(1, 2, 3, 0, ZoneOffset.UTC);
final var time2 = OffsetTime.of(1, 1, 1, 0, ZoneOffset.UTC);
System.out.println(time1); // 01:02:03Z
System.out.println(time2); // 01:01:01Z
System.out.println(time1.until(time2, ChronoUnit.HOURS)); // 0
System.out.println(time1.until(time2, ChronoUnit.MINUTES)); // -1
System.out.println(time1.until(time2, ChronoUnit.SECONDS)); // -62
System.out.println(time1.until(time2, ChronoUnit.NANOS)); // -62000000000
final var time1 = OffsetTime.of(1, 1, 1, 0, ZoneOffset.ofHours(3));
final var time2 = OffsetTime.of(1, 1, 1, 0, ZoneOffset.ofHours(-5));
System.out.println(time1); // 01:01:01+03:00
System.out.println(time2); // 01:01:01-05:00
System.out.println(time1.until(time2, ChronoUnit.HOURS)); // 8
System.out.println(time1.until(time2, ChronoUnit.MINUTES)); // 480
System.out.println(time1.until(time2, ChronoUnit.SECONDS)); // 28800
System.out.println(time1.until(time2, ChronoUnit.NANOS)); // 28800000000000
OffsetTime with (TemporalAdjuster adjuster)
final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z
final var adjuster = new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
return temporal.with(ChronoField.SECOND_OF_MINUTE, 59);
}
};
System.out.println(time.with(adjuster)); // 12:30:59Z
OffsetTime with (TemporalField field, long newValue)
final var time = OffsetTime.of(12, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 12:30:15.000000123Z
final var ret1 = time.with(ChronoField.HOUR_OF_DAY, 23);
System.out.println(ret1); // 23:30:15.000000123Z
final var ret2 = time.with(ChronoField.MINUTE_OF_HOUR, 59);
System.out.println(ret2); // 12:59:15.000000123Z
final var ret3 = time.with(ChronoField.SECOND_OF_MINUTE, 30);
System.out.println(ret3); // 12:30:30.000000123Z
final var ret4 = time.with(ChronoField.NANO_OF_SECOND, 456);
System.out.println(ret4); // 12:30:15.000000456Z
final var ret5 = time.with(ChronoField.OFFSET_SECONDS, 3600 * 8);
System.out.println(ret5); // 12:30:15.000000123+08:00
OffsetTime withHour (int hour)
final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z
System.out.println(time.withHour(23)); // 23:30:15Z
OffsetTime withMinute (int minute)
final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z
System.out.println(time.withMinute(59)); // 12:59:15Z
OffsetTime withNano (int nanoOfSecond)
final var time = OffsetTime.of(12, 30, 15, 123, ZoneOffset.UTC);
System.out.println(time); // 12:30:15.000000123Z
System.out.println(time.withNano(456789)); // 12:30:15.000456789Z
OffsetTime withOffsetSameInstant (ZoneOffset offset)
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z
final var ret = time.withOffsetSameInstant(ZoneOffset.ofHours(4));
System.out.println(ret); // 16:30+04:00
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:30+04:00
final var ret = time.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(ret); // 08:30Z
OffsetTime withOffsetSameLocal (ZoneOffset offset)
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30Z
final var ret = time.withOffsetSameLocal(ZoneOffset.ofHours(4));
System.out.println(ret); // 12:30+04:00
final var time = OffsetTime.of(12, 30, 0, 0, ZoneOffset.ofHours(4));
System.out.println(time); // 12:30+04:00
final var ret = time.withOffsetSameLocal(ZoneOffset.UTC);
System.out.println(ret); // 12:30Z
OffsetTime withSecond (int second)
final var time = OffsetTime.of(12, 30, 15, 0, ZoneOffset.UTC);
System.out.println(time); // 12:30:15Z
System.out.println(time.withSecond(59)); // 12:30:59Z
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset