Java : LocalTime with Examples
LocalTime (Java SE 21 & JDK 21) with Examples.
You will find code examples on most LocalTime methods.
Summary
final var time = LocalTime.of(13, 30, 15, 123000000);
System.out.println(time); // 13:30:15.123
System.out.println(time.getHour()); // 13
System.out.println(time.getMinute()); // 30
System.out.println(time.getSecond()); // 15
System.out.println(time.getNano()); // 123000000
Fields
static final LocalTime MAX
System.out.println(LocalTime.MAX); // 23:59:59.999999999
System.out.println(LocalTime.MAX.getHour()); // 23
System.out.println(LocalTime.MAX.getMinute()); // 59
System.out.println(LocalTime.MAX.getSecond()); // 59
System.out.println(LocalTime.MAX.getNano()); // 999999999
static final LocalTime MIDNIGHT
System.out.println(LocalTime.MIDNIGHT); // 00:00
static final LocalTime MIN
System.out.println(LocalTime.MIN); // 00:00
System.out.println(LocalTime.MIN.getHour()); // 0
System.out.println(LocalTime.MIN.getMinute()); // 0
System.out.println(LocalTime.MIN.getSecond()); // 0
System.out.println(LocalTime.MIN.getNano()); // 0
static final LocalTime NOON
System.out.println(LocalTime.NOON); // 12:00
Methods
Temporal adjustInto (Temporal temporal)
final var temporal = LocalDateTime.of(2100, 4, 12, 12, 30);
System.out.println(temporal); // 2100-04-12T12:30
final var time = LocalTime.of(13, 10, 20);
System.out.println(time); // 13:10:20
final var result = time.adjustInto(temporal);
System.out.println(result); // 2100-04-12T13:10:20
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var temporal = ZonedDateTime.of(2100, 4, 12, 12, 30, 10, 0, zone);
System.out.println(temporal); // 2100-04-12T12:30:10-07:00[America/Los_Angeles]
final var time = LocalTime.of(23, 50, 45, 123456);
System.out.println(time); // 23:50:45.000123456
final var result = time.adjustInto(temporal);
System.out.println(result); // 2100-04-12T23:50:45.000123456-07:00[America/Los_Angeles]
LocalDateTime atDate (LocalDate date)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.atDate(LocalDate.of(2100, 4, 12))); // 2100-04-12T13:30:15
System.out.println(time.atDate(LocalDate.of(-900, 1, 1))); // -0900-01-01T13:30:15
OffsetTime atOffset (ZoneOffset offset)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
{
final var offset = ZoneOffset.UTC;
System.out.println(offset); // Z
System.out.println(time.atOffset(offset)); // 13:30:15Z
}
{
final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00
System.out.println(time.atOffset(offset)); // 13:30:15+09:00
}
{
final var offset = ZoneOffset.ofHoursMinutes(-2, -30);
System.out.println(offset); // -02:30
System.out.println(time.atOffset(offset)); // 13:30:15-02:30
}
int compareTo (LocalTime other)
final var time1 = LocalTime.of(13, 30, 15);
System.out.println(time1); // 13:30:15
final var time2 = LocalTime.of(14, 30, 15);
System.out.println(time2); // 14:30:15
System.out.println(time1.compareTo(time2)); // -1
final var time1 = LocalTime.of(13, 31, 0);
System.out.println(time1); // 13:31
final var time2 = LocalTime.of(13, 30, 59);
System.out.println(time2); // 13:30:59
System.out.println(time1.compareTo(time2)); // 1
final var time1 = LocalTime.of(23, 30, 0);
System.out.println(time1); // 23:30
final var time2 = LocalTime.of(23, 30, 0);
System.out.println(time2); // 23:30
System.out.println(time1.compareTo(time2)); // 0
boolean equals (Object obj)
final var time1 = LocalTime.of(13, 30, 15);
System.out.println(time1); // 13:30:15
final var time2 = LocalTime.of(14, 30, 15);
System.out.println(time2); // 14:30:15
System.out.println(time1.equals(time2)); // false
final var time1 = LocalTime.of(13, 31, 0);
System.out.println(time1); // 13:31
final var time2 = LocalTime.of(13, 30, 59);
System.out.println(time2); // 13:30:59
System.out.println(time1.equals(time2)); // false
final var time1 = LocalTime.of(23, 30, 0);
System.out.println(time1); // 23:30
final var time2 = LocalTime.of(23, 30, 0);
System.out.println(time2); // 23:30
System.out.println(time1.equals(time2)); // true
String format (DateTimeFormatter formatter)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
{
final var ret1 = time.format(DateTimeFormatter.ISO_TIME);
System.out.println(ret1); // 13:30:15
final var ret2 = time.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
System.out.println(ret2); // 13:30:15
}
{
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 ret = time.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
} catch (DateTimeException e) {
System.out.println("DateTimeException! : " + e.getMessage());
}
// Result
// ↓
//DateTimeException! : Unsupported field: YearOfEra
}
static LocalTime from (TemporalAccessor temporal)
final var temporal = LocalDateTime.of(2100, 1, 3, 12, 30, 55);
System.out.println(temporal); // 2100-01-03T12:30:55
System.out.println(LocalTime.from(temporal)); // 12:30:55
final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles
final var temporal = ZonedDateTime.of(2100, 1, 3, 1, 2, 3, 456, zone);
System.out.println(temporal); // 2100-01-03T01:02:03.000000456-08:00[America/Los_Angeles]
System.out.println(LocalTime.from(temporal)); // 01:02:03.000000456
final var offset = ZoneOffset.ofHours(9);
System.out.println(offset); // +09:00
final var temporal = OffsetDateTime.of(1900, 12, 15, 4, 5, 6, 123000, offset);
System.out.println(temporal); // 1900-12-15T04:05:06.000123+09:00
System.out.println(LocalTime.from(temporal)); // 04:05:06.000123
int get (TemporalField field)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(time.get(ChronoField.HOUR_OF_DAY)); // 13
System.out.println(time.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(time.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(time.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(time.get(ChronoField.SECOND_OF_MINUTE)); // 15
System.out.println(time.isSupported(ChronoField.NANO_OF_SECOND)); // true
System.out.println(time.get(ChronoField.NANO_OF_SECOND)); // 123
final var time = LocalTime.of(1, 2, 3);
try {
final var ret = time.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
int getHour ()
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.getHour()); // 13
long getLong (TemporalField field)
final var time = LocalTime.of(13, 30, 15, 123);
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
int getMinute ()
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.getMinute()); // 30
int getNano ()
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.getNano()); // 0
final var time = LocalTime.of(13, 30, 15, 123456789);
System.out.println(time); // 13:30:15.123456789
System.out.println(time.getNano()); // 123456789
int getSecond ()
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.getSecond()); // 15
int hashCode ()
System.out.println(LocalTime.of(0, 0, 0).hashCode()); // 0
System.out.println(LocalTime.of(13, 30, 15).hashCode()); // 265185847
System.out.println(LocalTime.of(13, 30, 15, 0).hashCode()); // 265185847
System.out.println(LocalTime.of(13, 30, 15, 123).hashCode()); // 265185868
boolean isAfter (LocalTime other)
final var time = LocalTime.of(12, 1);
System.out.println(time); // 12:01
final var time2 = LocalTime.of(12, 0);
final var time3 = LocalTime.of(12, 1);
final var time4 = LocalTime.of(12, 2);
System.out.println(time2); // 12:00
System.out.println(time3); // 12:01
System.out.println(time4); // 12:02
System.out.println(time.isAfter(time2)); // true
System.out.println(time.isAfter(time3)); // false
System.out.println(time.isAfter(time4)); // false
final var time = LocalTime.of(0, 0, 0, 1);
System.out.println(time); // 00:00:00.000000001
final var time2 = LocalTime.of(0, 0, 0);
final var time3 = LocalTime.of(0, 0, 1);
final var time4 = LocalTime.of(23, 59, 59);
System.out.println(time2); // 00:00
System.out.println(time3); // 00:00:01
System.out.println(time4); // 23:59:59
System.out.println(time.isAfter(time2)); // true
System.out.println(time.isAfter(time3)); // false
System.out.println(time.isAfter(time4)); // false
boolean isBefore (LocalTime other)
final var time = LocalTime.of(12, 1);
System.out.println(time); // 12:01
final var time2 = LocalTime.of(12, 0);
final var time3 = LocalTime.of(12, 1);
final var time4 = LocalTime.of(12, 2);
System.out.println(time2); // 12:00
System.out.println(time3); // 12:01
System.out.println(time4); // 12:02
System.out.println(time.isBefore(time2)); // false
System.out.println(time.isBefore(time3)); // false
System.out.println(time.isBefore(time4)); // true
final var time = LocalTime.of(0, 0, 0, 1);
System.out.println(time); // 00:00:00.000000001
final var time2 = LocalTime.of(0, 0, 0);
final var time3 = LocalTime.of(0, 0, 1);
final var time4 = LocalTime.of(23, 59, 59);
System.out.println(time2); // 00:00
System.out.println(time3); // 00:00:01
System.out.println(time4); // 23:59:59
System.out.println(time.isBefore(time2)); // false
System.out.println(time.isBefore(time3)); // true
System.out.println(time.isBefore(time4)); // true
boolean isSupported (TemporalField field)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time.isSupported(ChronoField.HOUR_OF_DAY)); // true
System.out.println(time.get(ChronoField.HOUR_OF_DAY)); // 13
System.out.println(time.isSupported(ChronoField.MINUTE_OF_HOUR)); // true
System.out.println(time.get(ChronoField.MINUTE_OF_HOUR)); // 30
System.out.println(time.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
System.out.println(time.get(ChronoField.SECOND_OF_MINUTE)); // 15
System.out.println(time.isSupported(ChronoField.NANO_OF_SECOND)); // true
System.out.println(time.get(ChronoField.NANO_OF_SECOND)); // 123
final var time = LocalTime.of(1, 2, 3);
try {
final var ret = time.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
boolean isSupported (TemporalUnit unit)
final var time = LocalTime.of(13, 30, 15, 123);
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
LocalTime minus (long amountToSubtract, TemporalUnit unit)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time); // 13:30:15.000000123
System.out.println(time.minus(1, ChronoUnit.HOURS)); // 12:30:15.000000123
System.out.println(time.minus(2, ChronoUnit.MINUTES)); // 13:28:15.000000123
System.out.println(time.minus(3, ChronoUnit.SECONDS)); // 13:30:12.000000123
System.out.println(time.minus(4, ChronoUnit.NANOS)); // 13:30:15.000000119
LocalTime minus (TemporalAmount amountToSubtract)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time); // 13:30:15.000000123
System.out.println(time.minus(Duration.ofHours(1))); // 12:30:15.000000123
System.out.println(time.minus(Duration.ofMinutes(2))); // 13:28:15.000000123
System.out.println(time.minus(Duration.ofSeconds(3))); // 13:30:12.000000123
System.out.println(time.minus(Duration.ofNanos(4))); // 13:30:15.000000119
LocalTime minusHours (long hoursToSubtract)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.minusHours(1)); // 12:30:15
System.out.println(time.minusHours(2)); // 11:30:15
System.out.println(time.minusHours(13)); // 00:30:15
System.out.println(time.minusHours(14)); // 23:30:15
LocalTime minusMinutes (long minutesToSubtract)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.minusMinutes(1)); // 13:29:15
System.out.println(time.minusMinutes(2)); // 13:28:15
System.out.println(time.minusMinutes(30)); // 13:00:15
System.out.println(time.minusMinutes(31)); // 12:59:15
System.out.println(time.minusMinutes(60 * 13)); // 00:30:15
System.out.println(time.minusMinutes(60 * 14)); // 23:30:15
LocalTime minusNanos (long nanosToSubtract)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time); // 13:30:15.000000123
System.out.println(time.minusNanos(1L)); // 13:30:15.000000122
System.out.println(time.minusNanos(2L)); // 13:30:15.000000121
System.out.println(time.minusNanos(123L)); // 13:30:15
System.out.println(time.minusNanos(124L)); // 13:30:14.999999999
LocalTime minusSeconds (long secondsToSubtract)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.minusSeconds(1)); // 13:30:14
System.out.println(time.minusSeconds(2)); // 13:30:13
System.out.println(time.minusSeconds(15)); // 13:30
System.out.println(time.minusSeconds(16)); // 13:29:59
System.out.println(time.minusSeconds(76)); // 13:28:59
System.out.println(time.minusSeconds(60 * 30)); // 13:00:15
System.out.println(time.minusSeconds(60 * 31)); // 12:59:15
static LocalTime now ()
final var now = LocalTime.now();
System.out.println(now); // 14:42:07.212227100
static LocalTime now (Clock clock)
// A clock advanced 6 hours.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofHours(6));
System.out.println(LocalTime.now()); // 16:13:32.904635100
System.out.println(LocalTime.now(clock)); // 22:13:32.906633900
static LocalTime now (ZoneId zone)
final var zone1 = ZoneId.systemDefault();
System.out.println(zone1); // America/Los_Angeles
final var zone2 = ZoneId.of("Asia/Tokyo");
System.out.println(zone2); // Asia/Tokyo
System.out.println(LocalTime.now(zone1)); // 00:24:41.670676100
System.out.println(LocalTime.now(zone2)); // 16:24:41.671674600
static LocalTime of (int hour, int minute)
System.out.println(LocalTime.of(0, 0)); // 00:00
System.out.println(LocalTime.of(12, 30)); // 12:30
System.out.println(LocalTime.of(23, 59)); // 23:59
static LocalTime of (int hour, int minute, int second)
System.out.println(LocalTime.of(0, 0, 0)); // 00:00
System.out.println(LocalTime.of(12, 30, 15)); // 12:30:15
System.out.println(LocalTime.of(23, 59, 59)); // 23:59:59
static LocalTime of (int hour, int minute, int second, int nanoOfSecond)
System.out.println(LocalTime.of(0, 0, 0, 0)); // 00:00
System.out.println(LocalTime.of(12, 30, 15, 12345)); // 12:30:15.000012345
System.out.println(LocalTime.of(23, 59, 59, 999999999)); // 23:59:59.999999999
static LocalTime ofInstant (Instant instant, ZoneId zone)
final var instant = Instant.ofEpochSecond(4102488000L);
System.out.println(instant); // 2100-01-01T12:00:00Z
System.out.println(LocalTime.ofInstant(instant, ZoneOffset.UTC)); // 12:00
System.out.println(LocalTime.ofInstant(instant, ZoneOffset.ofHours(8))); // 20:00
System.out.println(LocalTime.ofInstant(instant, ZoneOffset.ofHours(-8))); // 04:00
static LocalTime ofNanoOfDay (long nanoOfDay)
System.out.println(LocalTime.ofNanoOfDay(0L)); // 00:00
System.out.println(LocalTime.ofNanoOfDay(123L)); // 00:00:00.000000123
System.out.println(LocalTime.ofNanoOfDay(1000L)); // 00:00:00.000001
System.out.println(LocalTime.ofNanoOfDay(1000000L)); // 00:00:00.001
System.out.println(LocalTime.ofNanoOfDay(1000000000L)); // 00:00:01
System.out.println(LocalTime.ofNanoOfDay(1000000000L * 60)); // 00:01
System.out.println(LocalTime.ofNanoOfDay(1000000000L * 60 * 60 * 23)); // 23:00
static LocalTime ofSecondOfDay (long secondOfDay)
System.out.println(LocalTime.ofSecondOfDay(0L)); // 00:00
System.out.println(LocalTime.ofSecondOfDay(59L)); // 00:00:59
System.out.println(LocalTime.ofSecondOfDay(60L)); // 00:01
System.out.println(LocalTime.ofSecondOfDay(61L)); // 00:01:01
System.out.println(LocalTime.ofSecondOfDay(62L)); // 00:01:02
System.out.println(LocalTime.ofSecondOfDay(60L * 60)); // 01:00
System.out.println(LocalTime.ofSecondOfDay(60L * 60 * 23)); // 23:00
static LocalTime parse (CharSequence text)
System.out.println(LocalTime.parse("12:30")); // 12:30
System.out.println(LocalTime.parse("23:59:59.123456789")); // 23:59:59.123456789
System.out.println(LocalTime.parse("02:05:01")); // 02:05:01
try {
final var ret = LocalTime.parse("2:5:1");
} catch (DateTimeParseException e) {
System.out.println("DateTimeParseException! : " + e.getMessage());
}
// Result
// ↓
//DateTimeParseException! : Text '2:5:1' could not be parsed at index 0
static LocalTime parse (CharSequence text, DateTimeFormatter formatter)
final var ret1 = LocalTime.parse("12:30:15", DateTimeFormatter.ISO_LOCAL_TIME);
System.out.println(ret1); // 12:30:15
final var ret2 = LocalTime.parse("2:5:1", DateTimeFormatter.ofPattern("H:m:s"));
System.out.println(ret2); // 02:05:01
final var ret3 = LocalTime.parse("1:30:15 PM",
DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
System.out.println(ret3); // 13:30:15
LocalTime plus (long amountToAdd, TemporalUnit unit)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time); // 13:30:15.000000123
System.out.println(time.plus(1, ChronoUnit.HOURS)); // 14:30:15.000000123
System.out.println(time.plus(2, ChronoUnit.MINUTES)); // 13:32:15.000000123
System.out.println(time.plus(3, ChronoUnit.SECONDS)); // 13:30:18.000000123
System.out.println(time.plus(4, ChronoUnit.NANOS)); // 13:30:15.000000127
LocalTime plus (TemporalAmount amountToAdd)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time); // 13:30:15.000000123
System.out.println(time.plus(Duration.ofHours(1))); // 14:30:15.000000123
System.out.println(time.plus(Duration.ofMinutes(2))); // 13:32:15.000000123
System.out.println(time.plus(Duration.ofSeconds(3))); // 13:30:18.000000123
System.out.println(time.plus(Duration.ofNanos(4))); // 13:30:15.000000127
LocalTime plusHours (long hoursToAdd)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.plusHours(1)); // 14:30:15
System.out.println(time.plusHours(2)); // 15:30:15
System.out.println(time.plusHours(10)); // 23:30:15
System.out.println(time.plusHours(11)); // 00:30:15
LocalTime plusMinutes (long minutesToAdd)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.plusMinutes(1)); // 13:31:15
System.out.println(time.plusMinutes(2)); // 13:32:15
System.out.println(time.plusMinutes(29)); // 13:59:15
System.out.println(time.plusMinutes(30)); // 14:00:15
System.out.println(time.plusMinutes(31)); // 14:01:15
System.out.println(time.plusMinutes(60 * 10)); // 23:30:15
System.out.println(time.plusMinutes(60 * 11)); // 00:30:15
LocalTime plusNanos (long nanosToAdd)
final var time = LocalTime.of(13, 30, 15, 123);
System.out.println(time); // 13:30:15.000000123
System.out.println(time.plusNanos(1L)); // 13:30:15.000000124
System.out.println(time.plusNanos(2L)); // 13:30:15.000000125
System.out.println(time.plusNanos(999999999L - 123)); // 13:30:15.999999999
System.out.println(time.plusNanos(999999999L - 122)); // 13:30:16
LocalTime plusSeconds (long secondstoAdd)
final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.plusSeconds(1)); // 13:30:16
System.out.println(time.plusSeconds(2)); // 13:30:17
System.out.println(time.plusSeconds(44)); // 13:30:59
System.out.println(time.plusSeconds(45)); // 13:31
System.out.println(time.plusSeconds(46)); // 13:31:01
System.out.println(time.plusSeconds(60 * 29)); // 13:59:15
System.out.println(time.plusSeconds(60 * 30)); // 14:00:15
<R> R query (TemporalQuery<R> query)
final var time = LocalTime.of(12, 30, 20, 123456789);
System.out.println(time); // 12:30:20.123456789
System.out.println(time.query(TemporalQueries.precision())); // Nanos
ValueRange range (TemporalField field)
final var time = LocalTime.of(0, 0);
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
long toEpochSecond (LocalDate date, ZoneOffset offset)
final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30
final var date = LocalDate.of(2100, 4, 8);
System.out.println(date); // 2100-04-08
final long epochSecond1 = time.toEpochSecond(date, ZoneOffset.UTC);
System.out.println(epochSecond1); // 4110870600
System.out.println(Instant.ofEpochSecond(epochSecond1)); // 2100-04-08T12:30:00Z
final long epochSecond2 = time.toEpochSecond(date, ZoneOffset.ofHours(8));
System.out.println(epochSecond2); // 4110841800
System.out.println(Instant.ofEpochSecond(epochSecond2)); // 2100-04-08T04:30:00Z
long toNanoOfDay ()
final var time = LocalTime.of(0, 0);
System.out.println(time); // 00:00
System.out.println(time.toNanoOfDay()); // 0
final var time = LocalTime.of(0, 0, 0, 123456789);
System.out.println(time); // 00:00:00.123456789
System.out.println(time.toNanoOfDay()); // 123456789
final var time = LocalTime.of(0, 0, 1, 123);
System.out.println(time); // 00:00:01.000000123
System.out.println(time.toNanoOfDay()); // 1000000123
final var time = LocalTime.of(0, 2, 1, 123);
System.out.println(time); // 00:02:01.000000123
System.out.println(time.toNanoOfDay()); // 121000000123
final var time = LocalTime.MAX;
System.out.println(time); // 23:59:59.999999999
System.out.println(time.toNanoOfDay()); // 86399999999999
int toSecondOfDay ()
final var time = LocalTime.of(0, 0);
System.out.println(time); // 00:00
System.out.println(time.toSecondOfDay()); // 0
final var time = LocalTime.of(0, 0, 1);
System.out.println(time); // 00:00:01
System.out.println(time.toSecondOfDay()); // 1
final var time = LocalTime.of(0, 0, 2);
System.out.println(time); // 00:00:02
System.out.println(time.toSecondOfDay()); // 2
final var time = LocalTime.of(0, 1, 0);
System.out.println(time); // 00:01
System.out.println(time.toSecondOfDay()); // 60
final var time = LocalTime.of(1, 0, 0);
System.out.println(time); // 01:00
System.out.println(time.toSecondOfDay()); // 3600
final var time = LocalTime.MAX;
System.out.println(time); // 23:59:59.999999999
System.out.println(time.toSecondOfDay()); // 86399
String toString ()
final var time = LocalTime.of(0, 0);
final var str = time.toString();
System.out.println(str); // 00:00
final var time = LocalTime.of(1, 2, 3);
final var str = time.toString();
System.out.println(str); // 01:02:03
final var time = LocalTime.of(12, 30, 15, 123);
final var str = time.toString();
System.out.println(str); // 12:30:15.000000123
final var time = LocalTime.MAX;
final var str = time.toString();
System.out.println(str); // 23:59:59.999999999
LocalTime truncatedTo (TemporalUnit unit)
final var time = LocalTime.of(12, 30, 15, 123456789);
System.out.println(time); // 12:30:15.123456789
System.out.println(time.truncatedTo(ChronoUnit.HOURS)); // 12:00
System.out.println(time.truncatedTo(ChronoUnit.MINUTES)); // 12:30
System.out.println(time.truncatedTo(ChronoUnit.SECONDS)); // 12:30:15
System.out.println(time.truncatedTo(ChronoUnit.NANOS)); // 12:30:15.123456789
long until (Temporal endExclusive, TemporalUnit unit)
final var time1 = LocalTime.of(1, 2);
final var time2 = LocalTime.of(3, 32, 15, 123);
System.out.println(time1); // 01:02
System.out.println(time2); // 03:32:15.000000123
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 = LocalTime.of(1, 2, 3);
final var time2 = LocalTime.of(1, 1, 1);
System.out.println(time1); // 01:02:03
System.out.println(time2); // 01:01:01
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
LocalTime with (TemporalAdjuster adjuster)
final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30
final var adjuster = new TemporalAdjuster() {
@Override
public Temporal adjustInto(Temporal temporal) {
final var hours = temporal.get(ChronoField.HOUR_OF_DAY);
final var minutes = temporal.get(ChronoField.MINUTE_OF_HOUR);
return temporal
.with(ChronoField.HOUR_OF_DAY, hours / 2)
.with(ChronoField.MINUTE_OF_HOUR, minutes / 2);
}
};
System.out.println(time.with(adjuster)); // 06:15
LocalTime with (TemporalField field, long newValue)
final var time = LocalTime.of(12, 30, 15, 123);
System.out.println(time); // 12:30:15.000000123
System.out.println(time.with(ChronoField.HOUR_OF_DAY, 23)); // 23:30:15.000000123
System.out.println(time.with(ChronoField.MINUTE_OF_HOUR, 59)); // 12:59:15.000000123
System.out.println(time.with(ChronoField.SECOND_OF_MINUTE, 30)); // 12:30:30.000000123
System.out.println(time.with(ChronoField.NANO_OF_SECOND, 456)); // 12:30:15.000000456
LocalTime withHour (int hour)
final var time = LocalTime.of(12, 30, 15);
System.out.println(time); // 12:30:15
System.out.println(time.withHour(23)); // 23:30:15
LocalTime withMinute (int minute)
final var time = LocalTime.of(12, 30, 15);
System.out.println(time); // 12:30:15
System.out.println(time.withMinute(59)); // 12:59:15
LocalTime withNano (int nanoOfSecond)
final var time = LocalTime.of(12, 30, 15, 123);
System.out.println(time); // 12:30:15.000000123
System.out.println(time.withNano(456789)); // 12:30:15.000456789
LocalTime withSecond (int second)
final var time = LocalTime.of(12, 30, 15);
System.out.println(time); // 12:30:15
System.out.println(time.withSecond(59)); // 12:30:59
Related posts
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset