Java : LocalTime with Examples

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


Summary

A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.

Class diagram

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

The maximum supported LocalTime, '23:59:59.999999999'.

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

The time of midnight at the start of the day, '00:00'.

System.out.println(LocalTime.MIDNIGHT); // 00:00

static final LocalTime MIN

The minimum supported LocalTime, '00:00'.

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

The time of noon in the middle of the day, '12:00'.

System.out.println(LocalTime.NOON); // 12:00

Methods

Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object to have the same time as this object.

final var temporal = LocalDateTime.of(2021, 4, 12, 12, 30);
System.out.println(temporal); // 2021-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); // 2021-04-12T13:10:20
final var temporal = ZonedDateTime.of(2021, 4, 12, 12, 30, 10, 0, ZoneId.systemDefault());
System.out.println(temporal); // 2021-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); // 2021-04-12T23:50:45.000123456-07:00[America/Los_Angeles]

LocalDateTime atDate (LocalDate date)

Combines this time with a date to create a LocalDateTime.

final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15

System.out.println(time.atDate(LocalDate.of(2021, 4, 12))); // 2021-04-12T13:30:15
System.out.println(time.atDate(LocalDate.of(-900, 1, 1))); // -0900-01-01T13:30:15

OffsetTime atOffset (ZoneOffset offset)

Combines this time with an offset to create an OffsetTime.

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)

Compares this time to another time.

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)

Checks if this time is equal to another time.

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)

Formats this time using the specified 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()); // 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

    // FULL and LONG need the zone.
    //time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)); // DateTimeException
    //time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG)); // DateTimeException
}

static LocalTime from (TemporalAccessor temporal)

Obtains an instance of LocalTime from a temporal object.

final var temporal = LocalDateTime.of(2021, 1, 3, 12, 30, 55);
System.out.println(temporal); // 2021-01-03T12:30:55
System.out.println(LocalTime.from(temporal)); // 12:30:55
final var temporal = ZonedDateTime.of(2021, 1, 3, 1, 2, 3, 456, ZoneId.systemDefault());
System.out.println(temporal); // 2021-01-03T01:02:03.000000456-08:00[America/Los_Angeles]
System.out.println(LocalTime.from(temporal)); // 01:02:03.000000456
final var temporal = OffsetDateTime.of(1999, 12, 15, 4, 5, 6, 123000, ZoneOffset.ofHours(12));
System.out.println(temporal); // 1999-12-15T04:05:06.000123+12:00
System.out.println(LocalTime.from(temporal)); // 04:05:06.000123

int get (TemporalField field)

Gets the value of the specified field from this time as an int.

final var time = LocalTime.of(13, 30, 15, 123);

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

//time.get(ChronoField.YEAR); // UnsupportedTemporalTypeException

int getHour ()

Gets the hour-of-day field.

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)

Gets the value of the specified field from this time as a long.

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

//time.getLong(ChronoField.YEAR); // UnsupportedTemporalTypeException

int getMinute ()

Gets the minute-of-hour field.

final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.getMinute()); // 30

int getNano ()

Gets the nano-of-second field.

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 ()

Gets the second-of-minute field.

final var time = LocalTime.of(13, 30, 15);
System.out.println(time); // 13:30:15
System.out.println(time.getSecond()); // 15

int hashCode ()

A hash code for this time.

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)

Checks if this time is after the specified time.

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)

Checks if this time is before the specified time.

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)

Checks if the specified field is supported.

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

System.out.println(time.isSupported(ChronoField.YEAR)); // false
//time.get(ChronoField.YEAR); // UnsupportedTemporalTypeException

boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

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)

Returns a copy of this time with the specified amount subtracted.

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)

Returns a copy of this time with the specified amount subtracted.

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)

Returns a copy of this LocalTime with the specified number of hours subtracted.

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)

Returns a copy of this LocalTime with the specified number of minutes subtracted.

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)

Returns a copy of this LocalTime with the specified number of nanoseconds subtracted.

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)

Returns a copy of this LocalTime with the specified number of seconds subtracted.

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 ()

Obtains the current time from the system clock in the default time-zone.

System.out.println(LocalTime.now()); // 16:13:22.874390900

static LocalTime now (Clock clock)

Obtains the current time from the specified clock.

// Clock advanced for 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)

Obtains the current time from the system clock in the specified time-zone.

System.out.println(ZoneId.systemDefault()); // America/Los_Angeles

System.out.println(LocalTime.now()); // 00:35:24.522827300
System.out.println(LocalTime.now(ZoneOffset.UTC)); // 08:35:24.523826800

static LocalTime of (int hour, int minute)

Obtains an instance of LocalTime from an hour and 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

//LocalTime.of(24, 59); // DateTimeException: Invalid value for HourOfDay (valid values 0 - 23): 24
//LocalTime.of(0, 60); // DateTimeException: Invalid value for MinuteOfHour (valid values 0 - 59): 60

static LocalTime of (int hour, int minute, int second)

Obtains an instance of LocalTime from an hour, minute and 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

//LocalTime.of(24, 59, 0); // DateTimeException: Invalid value for HourOfDay (valid values 0 - 23): 24
//LocalTime.of(0, 60, 0); // DateTimeException: Invalid value for MinuteOfHour (valid values 0 - 59): 60
//LocalTime.of(0, 0, 60); // DateTimeException: Invalid value for SecondOfMinute (valid values 0 - 59): 60

static LocalTime of (int hour, int minute, int second, int nanoOfSecond)

Obtains an instance of LocalTime from an hour, minute, second and nanosecond.

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

//LocalTime.of(24, 59, 0, 0); // DateTimeException: Invalid value for HourOfDay (valid values 0 - 23): 24
//LocalTime.of(0, 60, 0, 0); // DateTimeException: Invalid value for MinuteOfHour (valid values 0 - 59): 60
//LocalTime.of(0, 0, 60, 0); // DateTimeException: Invalid value for SecondOfMinute (valid values 0 - 59): 60
//LocalTime.of(0, 0, 0, 1000000000); // DateTimeException: Invalid value for NanoOfSecond (valid values 0 - 999999999): 1000000000

static LocalTime ofInstant (Instant instant, ZoneId zone)

Obtains an instance of LocalTime from an Instant and zone ID.

final var instant = Instant.ofEpochSecond(1617883200);
System.out.println(instant); // 2021-04-08T12: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)

Obtains an instance of LocalTime from a nanos-of-day value.

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)

Obtains an instance of LocalTime from a second-of-day value.

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)

Obtains an instance of LocalTime from a text string such as 10:15.

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
//LocalTime.parse("2:5:1"); // DateTimeParseException

static LocalTime parse (CharSequence text, DateTimeFormatter formatter)

Obtains an instance of LocalTime from a text string using a specific 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)

Returns a copy of this time with the specified amount added.

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)

Returns a copy of this time with the specified amount added.

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)

Returns a copy of this LocalTime with the specified number of hours added.

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)

Returns a copy of this LocalTime with the specified number of minutes added.

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)

Returns a copy of this LocalTime with the specified number of nanoseconds added.

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)

Returns a copy of this LocalTime with the specified number of seconds added.

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)

Queries this time using the specified 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)

Gets the range of valid values for the specified 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

//time.range(ChronoField.YEAR); // UnsupportedTemporalTypeException

long toEpochSecond (LocalDate date, ZoneOffset offset)

Converts this LocalTime to the number of seconds since the epoch of 1970-01-01T00:00:00Z.

final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30

final var date = LocalDate.of(2021, 4, 8);
System.out.println(date); // 2021-04-08

final long epochSecond1 = time.toEpochSecond(date, ZoneOffset.UTC);
System.out.println(epochSecond1); // 1617885000
System.out.println(Instant.ofEpochSecond(epochSecond1)); // 2021-04-08T12:30:00Z

final long epochSecond2 = time.toEpochSecond(date, ZoneOffset.ofHours(8));
System.out.println(epochSecond2); // 1617856200
System.out.println(Instant.ofEpochSecond(epochSecond2)); // 2021-04-08T04:30:00Z

long toNanoOfDay ()

Extracts the time as nanos of day, from 0 to 24 * 60 * 60 * 1,000,000,000 - 1.

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 ()

Extracts the time as seconds of day, from 0 to 24 * 60 * 60 - 1.

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 ()

Outputs this time as a String, such as 10:15.

final var time = LocalTime.of(0, 0);
System.out.println(time.toString()); // 00:00
final var time = LocalTime.of(1, 2, 3);
System.out.println(time.toString()); // 01:02:03
final var time = LocalTime.of(12, 30, 15, 123);
System.out.println(time.toString()); // 12:30:15.000000123
final var time = LocalTime.MAX;
System.out.println(time.toString()); // 23:59:59.999999999

LocalTime truncatedTo (TemporalUnit unit)

Returns a copy of this LocalTime with the time truncated.

final var time = LocalTime.of(12, 30, 15, 123456789);
System.out.println(time.toString()); // 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

//time.truncatedTo(ChronoUnit.YEARS); // UnsupportedTemporalTypeException

long until (Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time until another time in terms of the specified 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)

Returns an adjusted copy of this time.

final var time = LocalTime.of(12, 30, 15);
System.out.println(time); // 12:30:15

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:59

LocalTime with (TemporalField field, long newValue)

Returns a copy of this time with the specified field set to a new value.

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)

Returns a copy of this LocalTime with the hour-of-day altered.

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)

Returns a copy of this LocalTime with the minute-of-hour altered.

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)

Returns a copy of this LocalTime with the nano-of-second altered.

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)

Returns a copy of this LocalTime with the second-of-minute altered.

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

To top of page