- LocalTime
- Static Factory Methods
- isAfter(..), isBefore(..)
- format(DateTimeFormatter)
- getX() *
- get(TemporalField)
- getLong(TemporalField)
- plusX(..) *
- plus(TemporalAmount)
- plus(long, TemporalUnit)
- minusX(..) *
- minus(TemporalAmount)
- minus(long, TemporalUnit)
- withX(..) *
- with(TemporalAdjuster)
- with(TemporalField, long)
- adjustInto(Temporal)
- atDate(LocalDate)
- atOffset(ZoneOffset)
- toX(..) *
- until(Temporal, TemporalUnit)
- truncatedTo(TemporalUnit)
- range(TemporalField)
- query(TemporalQuery<R>)
- isSupported(TemporalField)
- isSupported(TemporalUnit)
Java LocalTime Tutorial with Examples
1. LocalTime
The LocalTime class represents local time. It does not include date and time zone information. LocalTime includes hours, minutes, seconds, even nanoseconds (1 second = 1 billion nanoseconds).
- Java 8 Date Time API Overview
The LocalTime class is in the java.time package. Just like other classes introduced in Java 8 Date Time API, LocalTime is immutable, which means all computations on LocalTime create a new LocalTime object, so it's safe when used in multithreading environments.
public final class LocalTime
implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable
2. Static Factory Methods
No ADS
The LocalTime class does not provide any constructor, but it provides static factory methods to create new objects:
public static LocalTime now()
public static LocalTime now(ZoneId zone)
public static LocalTime now(Clock clock)
public static LocalTime of(int hour, int minute)
public static LocalTime of(int hour, int minute, int second)
public static LocalTime of(int hour, int minute, int second, int nanoOfSecond)
public static LocalTime ofInstant(Instant instant, ZoneId zone)
public static LocalTime ofSecondOfDay(long secondOfDay)
public static LocalTime ofNanoOfDay(long nanoOfDay)
public static LocalTime from(TemporalAccessor temporal)
public static LocalTime parse(CharSequence text)
public static LocalTime parse(CharSequence text, DateTimeFormatter formatter)
Create a LocalTime object representing the current time obtained from the system clock and default time zone (On your computer).
// Current time from the system clock in the default time-zone.
LocalTime localTime = LocalTime.now();
System.out.println("localTime: " + localTime);
Create a LocalTime object representing the current time from a specified clock:
// Clock from system default zone
Clock clockDefault = Clock.systemDefaultZone();
Clock clock = Clock.system(ZoneId.of("Europe/Paris"));
// Current time from the specified clock.
LocalTime localTime = LocalTime.now(clock);
System.out.println("localTime: " + localTime); // 19:51:11.990871 (Example)
- Java Clock
Create a LocalTime object representing the current time from a specified ZoneId:
// System default zone
ZoneId zoneIdDefault = ZoneId.systemDefault();
ZoneId zoneId = ZoneId.of("Australia/Sydney");
LocalTime localTime = LocalTime.now(zoneId);
System.out.println("Now is: " + localTime); // 03:56:06.006877 (Example)
Create a LocalTime object from the specified hour, minute, second, nanoOfSecond values:
// of(int hour, int minute, int second, int nanoOfSecond)
LocalTime localTime1 = LocalTime.of(13, 10, 20, 12345);
System.out.println("localTime1: " + localTime1); // 13:10:20.000012345
// of(int hour, int minute, int second)
LocalTime localTime2 = LocalTime.of(13, 10, 20);
System.out.println("localTime2: " + localTime2); // 13:10:20
// of(int hour, int minute)
LocalTime localTime3 = LocalTime.of(13, 10);
System.out.println("localTime3: " + localTime3); // 13:10
Create a LocalTime object with the specified Instant and ZoneId objects:
LocalTime_ofInstant_ex1.java
// ofInstant(Instant instant, ZoneId zone)
Instant instant = Instant.now();
ZoneId zoneId = ZoneId.systemDefault();
LocalTime localTime = LocalTime.ofInstant(instant, zoneId);
System.out.println("localTime: " + localTime);
- Java Instant
Create a LocalTime object from parsing a date-formatted text:
// parse(CharSequence text)
LocalTime localTime1 = LocalTime.parse("10:15:30"); // 10:15:30
System.out.println("localTime1: " + localTime1);
// parse(CharSequence text, DateTimeFormatter formatter)
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("HH:mm");
LocalTime localTime2 = LocalTime.parse("13:15", fmt2); // 13:15
System.out.println("localTime2: " + localTime2);
- Java DateTimeFormatter
- Java Date Time Format Pattern
Create a LocalTime object from a specified TemporalAccessor object.
// from(TemporalAccessor temporal)
TemporalAccessor nowDatetime = LocalDateTime.now();
System.out.println("LocalDateTime.now(): " + nowDatetime);
LocalTime nowTime = LocalTime.from(nowDatetime);
System.out.println("nowTime: " + nowTime);
3. isAfter(..), isBefore(..)
No ADS
The isBefore(LocalTime) method is used to check if this LocalTime object is before another given LocalTime object.
The isAfter(LocalTime) method is used to check if this LocalDate object is after another given LocalTime object.
public boolean isAfter(LocalTime other)
public boolean isBefore(LocalTime other)
localTime1 is before localTime2.
Example:
LocalTime_isBefore_isAfter_ex1.java
package org.o7planning.localtime.ex;
import java.time.LocalTime;
public class LocalTime_isBefore_isAfter_ex1 {
public static void main(String[] args) {
LocalTime localTime1 = LocalTime.parse("10:20"); // 10:20
LocalTime localTime2 = LocalTime.parse("21:15:30.12345"); // 21:15:30.123450
System.out.println("localTime1: " + localTime1); // 10:20
System.out.println("localTime2: " + localTime2); // 21:15:30.123450
System.out.println();
System.out.println("localTime1.isBefore(localTime2): " + localTime1.isBefore(localTime2)); // true
System.out.println("localTime1.isAfter(localTime2): " + localTime1.isAfter(localTime2)); // false
}
}
Output:
localTime1: 10:20
localTime2: 21:15:30.123450
localTime1.isBefore(localTime2): true
localTime1.isAfter(localTime2): false
4. format(DateTimeFormatter)
No ADS
Format this LocalTime object with a specified DateTimeFormatter.
public String format(DateTimeFormatter formatter)
Example:
LocalTime_format_ex1.java
LocalTime localTime = LocalTime.parse("10:15:30.12345");
System.out.println("localTime: " + localTime); // 10:15:30.123450
System.out.println();
// '10:15', '10:15:30' or '10:15:30+01:00'.
DateTimeFormatter fmt1 = DateTimeFormatter.ISO_TIME;
System.out.println("ISO_TIME: " + localTime.format(fmt1)); // 10:15:30.12345
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("HH:mm");
System.out.println("HH:mm: " + localTime.format(fmt2)); // 10:15
- Java DateTimeFormatter
- Java Date Time Format Pattern
5. getX() *
No ADS
The getNano() method returns the nanosecond part of this LocalDate. It is equivalent to calling the get(ChronoField.NANO_OF_SECOND) method. The return value is in the range from 0 to 10^9-1.
public int getHour()
public int getMinute()
public int getSecond()
public int getNano()
The methods getHour(), getMinute(), getSecond() are also understood in a similar way as the names imply.
Method | Same As | Range |
getNano() | get(ChronoField.NANO_OF_SECOND) | 0-10^9-1 |
getSecond() | get(ChronoField.SECOND_OF_MINUTE) | 0-59 |
getMinute() | get(ChronoField.MINUTE_OF_HOUR) | 0-59 |
getHour() | get(ChronoField.HOUR_OF_DAY) | 0-23 |
Example:
LocalTime_getX_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30.12345"); // 13:45:30.123450000
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
System.out.println("localTime.getHour(): " + localTime.getHour()); // 13
System.out.println("localTime.getMinute(): " + localTime.getMinute()); // 45
System.out.println("localTime.getSecond(): " + localTime.getSecond()); // 30
System.out.println("localTime.getNano(): " + localTime.getNano()); // 123450000
Output:
localTime: 13:45:30.123450
localTime.getHour(): 13
localTime.getMinute(): 45
localTime.getSecond(): 30
localTime.getNano(): 123450000
6. get(TemporalField)
No ADS
Return the value of the specified field of this LocalTime object as a 32-bit integer.
// Inherited from TemporalAccessor interface
public int get(TemporalField field)
Note: Some fields may not be supported by LocalTime and an UnsupportedTemporalTypeException will be thrown. To be sure, use the isSupported(TemporalField) method to check if a certain field is supported by LocalTime.
Example:
LocalTime_get_field_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime time = LocalTime.parse("13:45:30.12345"); // 13:45:30.123450000
System.out.println("localTime: " + time); // 13:45:30.123450
System.out.println();
// int get(TemporalField field)
System.out.println("AMPM_OF_DAY: " + time.get(ChronoField.AMPM_OF_DAY)); // 13
System.out.println("CLOCK_HOUR_OF_AMPM: " + time.get(ChronoField.CLOCK_HOUR_OF_AMPM)); // 1
System.out.println("CLOCK_HOUR_OF_DAY: " + time.get(ChronoField.CLOCK_HOUR_OF_DAY)); // 13
System.out.println("HOUR_OF_AMPM: " + time.get(ChronoField.HOUR_OF_AMPM)); // 1
System.out.println("HOUR_OF_DAY: " + time.get(ChronoField.HOUR_OF_DAY)); // 13
System.out.println("MILLI_OF_DAY: " + time.get(ChronoField.MILLI_OF_DAY)); // 49530123
System.out.println("MILLI_OF_SECOND: " + time.get(ChronoField.MILLI_OF_SECOND)); // 123
System.out.println("MINUTE_OF_DAY: " + time.get(ChronoField.MINUTE_OF_DAY)); // 825
System.out.println("MINUTE_OF_HOUR: " + time.get(ChronoField.MINUTE_OF_HOUR)); // 45
System.out.println("MICRO_OF_SECOND: " + time.get(ChronoField.MICRO_OF_SECOND)); // 123450
System.out.println("NANO_OF_SECOND: " + time.get(ChronoField.NANO_OF_SECOND)); // 123450000
System.out.println("SECOND_OF_DAY: " + time.get(ChronoField.SECOND_OF_DAY)); // 49530
System.out.println("SECOND_OF_MINUTE: " + time.get(ChronoField.SECOND_OF_MINUTE)); // 30
// Use getLong() instead
// System.out.println("NANO_OF_DAY: " + time.get(ChronoField.NANO_OF_DAY)); // 49530123450000
// Unsupported
// System.out.println("INSTANT_SECONDS: " + time.get(ChronoField.INSTANT_SECONDS));
// System.out.println("OFFSET_SECONDS: " + time.get(ChronoField.OFFSET_SECONDS));
- TemporalField
- ChronoField
7. getLong(TemporalField)
No ADS
Return the value of the specified field of this LocalTime object as a 64-bit integer.
// Inherited from TemporalAccessor interface
public long getLong(TemporalField field)
Note: Some fields may not be supported by LocalTime and an UnsupportedTemporalTypeException will be thrown. To be sure, use the isSupported(TemporalField) method to check if a certain field is supported by LocalTime.
Example:
LocalTime_getLong_field_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime time = LocalTime.parse("13:45:30.12345"); // 13:45:30.123450000
System.out.println("localTime: " + time); // 13:45:30.123450
System.out.println();
// long getLong(TemporalField field)
System.out.println("AMPM_OF_DAY: " + time.getLong(ChronoField.AMPM_OF_DAY)); // 13
System.out.println("CLOCK_HOUR_OF_AMPM: " + time.getLong(ChronoField.CLOCK_HOUR_OF_AMPM)); // 1
System.out.println("CLOCK_HOUR_OF_DAY: " + time.getLong(ChronoField.CLOCK_HOUR_OF_DAY)); // 13
System.out.println("HOUR_OF_AMPM: " + time.getLong(ChronoField.HOUR_OF_AMPM)); // 1
System.out.println("HOUR_OF_DAY: " + time.getLong(ChronoField.HOUR_OF_DAY)); // 13
System.out.println("MILLI_OF_DAY: " + time.getLong(ChronoField.MILLI_OF_DAY)); // 49530123
System.out.println("MILLI_OF_SECOND: " + time.getLong(ChronoField.MILLI_OF_SECOND)); // 123
System.out.println("MINUTE_OF_DAY: " + time.getLong(ChronoField.MINUTE_OF_DAY)); // 825
System.out.println("MINUTE_OF_HOUR: " + time.getLong(ChronoField.MINUTE_OF_HOUR)); // 45
System.out.println("MICRO_OF_SECOND: " + time.getLong(ChronoField.MICRO_OF_SECOND)); // 123450
System.out.println("NANO_OF_SECOND: " + time.getLong(ChronoField.NANO_OF_SECOND)); // 123450000
System.out.println("SECOND_OF_DAY: " + time.getLong(ChronoField.SECOND_OF_DAY)); // 49530
System.out.println("SECOND_OF_MINUTE: " + time.getLong(ChronoField.SECOND_OF_MINUTE)); // 30
System.out.println("NANO_OF_DAY: " + time.getLong(ChronoField.NANO_OF_DAY)); // 49530123450000
// Unsupported
// System.out.println("INSTANT_SECONDS: " + time.getLong(ChronoField.INSTANT_SECONDS));
// System.out.println("OFFSET_SECONDS: " + time.getLong(ChronoField.OFFSET_SECONDS));
- TemporalField
- ChronoField
8. plusX(..) *
No ADS
The plusHours(hoursToAdd) method returns a copy of this LocalTime object with the specified amount of hours added.
The methods plusMinutes(minutesToAdd), plusSeconds(secondsToAdd), plusNanos(nanosToAdd) are also understood as their names suggest.
public LocalTime plusHours(long hoursToAdd)
public LocalTime plusMinutes(long minutesToAdd)
public LocalTime plusSeconds(long secondstoAdd)
public LocalTime plusNanos(long nanosToAdd)
Example:
LocalTime_plusX_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30"); // 13:45:30
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
// plusHours(long hoursToAdd)
LocalTime localTime1 = localTime.plusHours(10);
System.out.println("localTime1: " + localTime1); // 23:45:30
// plusHours(long hoursToAdd)
LocalTime localTime2 = localTime.plusHours(20);
System.out.println("localTime2: " + localTime2); // 09:45:30
LocalTime localTime3 = localTime.plusHours(-10);
System.out.println("localTime3: " + localTime3); // 03:45:30
// plusMinutes(long minutesToAdd)
LocalTime localTime4 = localTime.plusMinutes(5);
System.out.println("localTime4: " + localTime4); // 13:50:30
// plusSeconds(long secondstoAdd)
LocalTime localTime5 = localTime.plusSeconds(29);
System.out.println("localTime5: " + localTime5); // 13:45:59
// plusNanos(long nanosToAdd)
LocalTime localTime6 = localTime.plusNanos(12345);
System.out.println("localTime6: " + localTime6); // 13:45:30.000012345
9. plus(TemporalAmount)
No ADS
Return a copy of this LocalTime object with the specified amount of time added.
// Inherited from Temporal interface
public LocalTime plus(TemporalAmount amountToAdd)
You can use the TemporalAmount.addTo(Temporal) method to get the same result.
// Defined in TemporalAmount interface:
public Temporal addTo(Temporal temporal)
Example:
LocalTime_plus_amount_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30"); // 13:45:30
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
// ofSeconds(long seconds, long nanoAdjustment)
TemporalAmount amount1 = Duration.ofSeconds(10, 12345);
//plus(TemporalAmount amountToAdd)
LocalTime localTime1 = localTime.plus(amount1);
System.out.println("localTime1: " + localTime1); // 13:45:40.000012345
// plus(TemporalAmount amountToAdd)
LocalTime from = LocalTime.of(10, 20, 35); // of(int hour, int minute, int second)
LocalTime to = LocalTime.of(10, 25, 45);
TemporalAmount amount2 = Duration.between(from, to); // 00:05:10
LocalTime localTime2 = localTime.plus(amount2);
System.out.println("localTime2: " + localTime2); // 13:50:40
- TemporalAmount
- Duration
10. plus(long, TemporalUnit)
No ADS
Return a copy of this LocalTime object with a specified value added in the given unit.
// Inherited from Temporal interface
public LocalTime plus(long amountToAdd, TemporalUnit unit)
Supported standard units:
Field | Same as |
NANOS | plusNanos(amountToAdd) |
MICROS | plusNanos(amountToAdd*1000) |
MILLIS | plusNanos(amountToAdd*100_000) |
SECONDS | plusSeconds(amountToAdd) |
MINUTES | plusMinutes(amountToAdd) |
HOURS | plusHours(amountToAdd) |
HALF_DAYS | plusHours(amountToAdd*12) |
Example:
LocalTime_plus_unit_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30"); // 13:45:30
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
// plus(long amountToAdd, TemporalUnit unit)
LocalTime localTime1 = localTime.plus(5, ChronoUnit.HOURS);
System.out.println("localTime1: " + localTime1); // 18:45:30
// plus(long amountToAdd, TemporalUnit unit)
LocalTime localTime2 = localTime.plus(10, ChronoUnit.SECONDS);
System.out.println("localTime2: " + localTime2); // 13:45:40
// plus(long amountToAdd, TemporalUnit unit)
LocalTime localTime3 = localTime.plus(12345, ChronoUnit.NANOS);
System.out.println("localTime3: " + localTime3); // 13:45:30.000012345
- TemporalUnit
- ChronoUnit
11. minusX(..) *
No ADS
The minusHours(hoursToSubtract) method returns a copy of this LocalTime object with the specified amount of hours subtracted.
The methods minusMinutes(minutesToSubtract), minusSeconds(secondsToSubtract), minusNanos(nanosToSubtract) are also understood as their names.
public LocalTime minusHours(long hoursToSubtract)
public LocalTime minusMinutes(long minutesToSubtract)
public LocalTime minusSeconds(long secondsToSubtract)
public LocalTime minusNanos(long nanosToSubtract)
Example:
LocalTime_minusX_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30"); // 13:45:30
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
// minusHours(long hoursToSubtract)
LocalTime localTime1 = localTime.minusHours(10);
System.out.println("localTime1: " + localTime1); // 03:45:30
// minusHours(long hoursToSubtract)
LocalTime localTime2 = localTime.minusHours(20);
System.out.println("localTime2: " + localTime2); // 17:45:30
LocalTime localTime3 = localTime.minusHours(-10);
System.out.println("localTime3: " + localTime3); // 23:45:30
// minusMinutes(long minutesToSubtract)
LocalTime localTime4 = localTime.minusMinutes(5);
System.out.println("localTime4: " + localTime4); // 13:40:30
// minusSeconds(long secondstoSubtract)
LocalTime localTime5 = localTime.minusSeconds(29);
System.out.println("localTime5: " + localTime5); // 13:45:01
// minusNanos(long nanosToSubtract)
LocalTime localTime6 = localTime.minusNanos(12345);
System.out.println("localTime6: " + localTime6); // 13:45:29.999987655
12. minus(TemporalAmount)
No ADS
Return a copy of this LocalTime object with the specified amount of time subtracted.
// Inherited from Temporal interface
public LocalTime minus(TemporalAmount amountToSubtract)
You can use the TemporalAmount.subtractFrom(Temporal) method to get the same result.
// Defined in TemporalAmount interface:
public Temporal subtractFrom(Temporal temporal)
Example:
LocalTime_minus_amount_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30"); // 13:45:30
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
// ofSeconds(long seconds, long nanoAdjustment)
TemporalAmount amount1 = Duration.ofSeconds(10, 12345);
// minus(TemporalAmount amountToSubtract)
LocalTime localTime1 = localTime.minus(amount1);
System.out.println("localTime1: " + localTime1); // 13:45:19.999987655
// minus(TemporalAmount amountToSubtract)
LocalTime from = LocalTime.of(10, 20, 35); // of(int hour, int minute, int second)
LocalTime to = LocalTime.of(10, 25, 45);
TemporalAmount amount2 = Duration.between(from, to); // 00:05:10
LocalTime localTime2 = localTime.minus(amount2);
System.out.println("localTime2: " + localTime2); // 13:40:20
- TemporalAmount
- Duration
13. minus(long, TemporalUnit)
No ADS
Return a copy of this LocalTime object with a specified value subtracted in the given unit.
// Inherited from Temporal interface
public LocalTime minus(long amountToSubtract, TemporalUnit unit)
Supported standard units:
Field | Same as |
NANOS | minusNanos(amountToSubtract) |
MICROS | minusNanos(amountToSubtract*1000) |
MILLIS | minusNanos(amountToSubtract*100_000) |
SECONDS | minusSeconds(amountToSubtract) |
MINUTES | minusMinutes(amountToSubtract) |
HOURS | minusHours(amountToSubtract) |
HALF_DAYS | minusHours(amountToSubtract*12) |
Example:
LocalTime_minus_unit_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30"); // 13:45:30
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
// minus(long amountToSubtract, TemporalUnit unit)
LocalTime localTime1 = localTime.minus(5, ChronoUnit.HOURS);
System.out.println("localTime1: " + localTime1); // 08:45:30
// minus(long amountToSubtract, TemporalUnit unit)
LocalTime localTime2 = localTime.minus(10, ChronoUnit.SECONDS);
System.out.println("localTime2: " + localTime2); // 13:45:20
// minus(long amountToSubtract, TemporalUnit unit)
LocalTime localTime3 = localTime.minus(12345, ChronoUnit.NANOS);
System.out.println("localTime3: " + localTime3); // 13:45:29.999987655
- TemporalUnit
- ChronoUnit
14. withX(..) *
No ADS
The withHour(hour) method returns a copy of this LocalTime with the hour changed to the specified hour.
The methods withMinute(minute), withSecond(second), withNano(nanoOfSecond) are also understood as the names imply.
public LocalTime withHour(int hour)
public LocalTime withMinute(int minute)
public LocalTime withSecond(int second)
public LocalTime withNano(int nanoOfSecond)
Example:
LocalTime_withX_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30"); // 13:45:30
System.out.println("localTime: " + localTime); // 13:45:30
System.out.println();
// withHour(int hour) [0 - 23]
LocalTime localTime1 = localTime.withHour(23);
System.out.println("localTime1: " + localTime1); // 23:45:30
// withMinute(int minute) [0 - 59]
LocalTime localTime2 = localTime.withMinute(59);
System.out.println("localTime2: " + localTime2); // 13:59:30
// withSecond(int second) [0 - 59]
LocalTime localTime3 = localTime.withSecond(59);
System.out.println("localTime3: " + localTime3); // 13:45:59
// withNano(int nanoOfSecond) [0 - 10^9-1]
LocalTime localTime4 = localTime.withNano(12345);
System.out.println("localTime4: " + localTime4); // 13:45:30.000012345
15. with(TemporalAdjuster)
No ADS
Return a copy of this LocalDate object, with time data adjusted by the TemporalAdjuster object.
// Inherited from Temporal interface
public LocalTime with(TemporalAdjuster adjuster)
Example:
LocalTime_with_adjuster_ex1.java
LocalTime now = LocalTime.now();
System.out.println("Now is: " + now);
System.out.println();
// with(TemporalAdjuster adjuster)
LocalTime noon = now.with(LocalTime.NOON);
System.out.println("NOON: " + noon); // 12:00
// with(TemporalAdjuster adjuster)
LocalTime midnight = now.with(LocalTime.MIDNIGHT);
System.out.println("MIDNIGHT: " + midnight); // 00:00
Example: Write a custom TemporalAdjuster:
LocalTime_with_adjuster_ex2.java
package org.o7planning.localtime.ex;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
public class LocalTime_with_adjuster_ex2 {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println("Now is: " + now);
System.out.println();
LocalTime lastMinOfHour = now.with(new LastMinOfHourAdjuster());
System.out.println("lastMinOfHour: " + lastMinOfHour);
}
}
class LastMinOfHourAdjuster implements TemporalAdjuster {
@Override
public Temporal adjustInto(Temporal temporal) {
LocalTime t = LocalTime.from(temporal);
return t.with(ChronoField.MINUTE_OF_HOUR, 59);
}
};
Output:
Now is: 19:37:57.524205
lastMinOfHour: 19:59:57.524205
16. with(TemporalField, long)
No ADS
Return a copy of this LocalTime object with the specified field changed to the new value.
// Inherited from Temporal interface
public LocalTime with(TemporalField field, long newValue)
Example:
LocalTime_with_field_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime myTime = LocalTime.parse("13:45:30.12345"); // 13:45:30.123450000
System.out.println("myTime is: " + myTime);
System.out.println("myTime CLOCK_HOUR_OF_AMPM: " + myTime.get(ChronoField.CLOCK_HOUR_OF_AMPM)); // 1
System.out.println("myTime CLOCK_HOUR_OF_DAY: " + myTime.get(ChronoField.CLOCK_HOUR_OF_DAY)); // 13
System.out.println("myTime SECOND_OF_MINUTE: " + myTime.get(ChronoField.SECOND_OF_MINUTE)); // 30
System.out.println();
LocalTime localTime1 = myTime.with(ChronoField.CLOCK_HOUR_OF_AMPM, 10); // 22:45:30.123450
System.out.println("localTime1: " + localTime1);
LocalTime localTime2 = myTime.with(ChronoField.CLOCK_HOUR_OF_DAY, 10); // 10:45:30.123450
System.out.println("localTime2: " + localTime2);
LocalTime localTime3 = myTime.with(ChronoField.SECOND_OF_MINUTE, 59); // 13:45:59.123450
System.out.println("localTime3: " + localTime3);
- ChronoField
- TemporalField
17. adjustInto(Temporal)
The adjustInto(Temporal) method returns a copy of the given Temporal object with time data adjusted to resemble this LocalTime object.
// Inherited from TemporalAdjuster interface
public Temporal adjustInto(Temporal temporal)
Example:
LocalTime localTime = LocalTime.parse("19:30:55");
// Current date time in System default time-zone.
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Before adjustInto(): " + zonedDateTime);
// returns Temporal
ZonedDateTime adjustedDateTime = (ZonedDateTime) localTime.adjustInto(zonedDateTime);
// print results
System.out.println("After adjustInto(): " + adjustedDateTime);
Output:
Before adjustInto(): 2021-06-18T17:18:06.355673+06:00[Asia/Bishkek]
After adjustInto(): 2021-06-18T19:30:55+06:00[Asia/Bishkek]
18. atDate(LocalDate)
The atDate(LocalDate) method is used to combine this LocalTime object with the specified LocalDate object to create a LocalDateTime object.
public LocalDateTime atDate(LocalDate date)
Example:
LocalTime_atDate_ex1.java
LocalTime localTime = LocalTime.parse("19:30:55");
LocalDate localDate = LocalDate.parse("2000-05-20");
LocalDateTime localDateTime = localTime.atDate(localDate);
System.out.println(localDateTime); // 2000-05-20T19:30:55
19. atOffset(ZoneOffset)
No ADS
The atOffset(ZoneOffset) method is used to combine this LocalTime object with the specified ZoneOffset object to create an OffsetTime object.
public OffsetTime atOffset(ZoneOffset offset)
Example:
LocalTime_atOffset_ex1.java
LocalTime localTime = LocalTime.parse("16:30:59");
System.out.println("localTime: " + localTime); // 16:30:59
// Create a ZoneOffset object
ZoneOffset offset = ZoneOffset.ofHours(2);
// atOffset(ZoneOffset)
OffsetTime offsetTime = localTime.atOffset(offset);
System.out.println("offsetTime: " + offsetTime); // 16:30:59+02:00
- Java OffsetTime
20. toX(..) *
public int toSecondOfDay()
public long toNanoOfDay()
public long toEpochSecond(LocalDate date, ZoneOffset offset)
21. until(Temporal, TemporalUnit)
Calculates the amount of time until another date-time in terms of the specified unit.
// Inherited from Temporal interface
public long until(Temporal endExclusive, TemporalUnit unit)
Example:
LocalTime_until_ex1.java
LocalTime localTime_from = LocalTime.of(1, 0, 0);
System.out.println("Time From: " + localTime_from); // 01:00
LocalTime localTime_to = LocalTime.of(13, 30, 59);
System.out.println("Time To: " + localTime_to); // 13:30:59
System.out.println();
long hours = localTime_from.until(localTime_to, ChronoUnit.HOURS);
System.out.println("hours: " + hours); // 12
long minutes = localTime_from.until(localTime_to, ChronoUnit.MINUTES);
System.out.println("minutes: " + minutes); // 750
You can also use the TemporalUnit.between(Temporal,Temporal) method to get the same result:
TemporalUnit_between_ex1.java
LocalTime localTime_from = LocalTime.of(1, 0, 0);
System.out.println("Time From: " + localTime_from); // 01:00
LocalTime localTime_to = LocalTime.of(13, 30, 59);
System.out.println("Time To: " + localTime_to); // 13:30:59
System.out.println();
long hours = ChronoUnit.HOURS.between(localTime_from, localTime_to);
System.out.println("hours: " + hours); // 12
long minutes = ChronoUnit.MINUTES.between(localTime_from, localTime_to);
System.out.println("minutes: " + minutes); // 750
- TemporalUnit
- ChronoUnit
- Temporal
22. truncatedTo(TemporalUnit)
Return a copy of this LocalTime object with the truncated time in the specified unit.
public LocalTime truncatedTo(TemporalUnit unit)
Example:
LocalTime_truncatedTo_ex1.java
LocalTime localTime = LocalTime.parse("21:30:59.12345");
System.out.println("localTime: " + localTime); // 21:30:59.123450
// truncatedTo(TemporalUnit unit)
LocalTime truncatedLocalTime = localTime.truncatedTo(ChronoUnit.MINUTES);
System.out.println("truncatedLocalTime: " + truncatedLocalTime); // 21:30
- TemporalUnit
- ChronoUnit
23. range(TemporalField)
Return a range of valid values for the specified field.
// Inherited from TemporalAccessor interface
public ValueRange range(TemporalField field)
Example:
LocalTime_range_ex1.java
LocalTime localTime = LocalTime.parse("09:30:45.12345");
// range(TemporalField field)
ValueRange range1 = localTime.range(ChronoField.MILLI_OF_SECOND);
System.out.println("Range in MILLI_OF_SECOND: " + range1);
ValueRange range2 = localTime.range(ChronoField.CLOCK_HOUR_OF_DAY);
System.out.println("Range in CLOCK_HOUR_OF_DAY: " + range2);
ValueRange range3 = localTime.range(ChronoField.CLOCK_HOUR_OF_AMPM);
System.out.println("Range in CLOCK_HOUR_OF_AMPM: " + range3);
Output:
Range in MILLI_OF_SECOND: 0 - 999
Range in CLOCK_HOUR_OF_DAY: 1 - 24
Range in CLOCK_HOUR_OF_DAY: 1 - 12
- ValueRange
- TemporalUnit
- ChronoField
24. query(TemporalQuery<R>)
No ADS
Query this LocalTime object with the specified TemporalQuery object to extract information.
// Inherited from TemporalAccessor interface
public <R> R query(TemporalQuery<R> query)
Example:
LocalTime_query_ex1.java
package org.o7planning.localtime.ex;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.chrono.Chronology;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalUnit;
public class LocalTime_query_ex1 {
public static void main(String[] args) {
LocalTime localTime = LocalTime.of(11, 30, 59, 12345);
System.out.printf("localTime: %s%n", localTime); // 11:30:59.000012345
TemporalUnit unit = localTime.query(TemporalQueries.precision());
System.out.printf("precision: %s%n", unit);
LocalTime localTime2 = localTime.query(TemporalQueries.localTime());
System.out.printf("localTime2: %s%n", localTime2);
LocalDate localDate = localTime.query(TemporalQueries.localDate());
System.out.printf("localDate: %s%n", localDate);
ZoneOffset zoneOffset = localTime.query(TemporalQueries.offset());
System.out.printf("ZoneOffset: %s%n", zoneOffset);
ZoneId zone = localTime.query(TemporalQueries.zone());
System.out.printf("zone: %s%n", zone);
ZoneId zoneId = localTime.query(TemporalQueries.zoneId());
System.out.printf("zoneId: %s%n", zoneId);
Chronology chronology = localTime.query(TemporalQueries.chronology());
System.out.printf("chronology: %s%n", chronology);
}
}
Output:
localTime: 11:30:59.000012345
precision: Nanos
localTime2: 11:30:59.000012345
localDate: null
ZoneOffset: null
zone: null
zoneId: null
chronology: null
25. isSupported(TemporalField)
No ADS
// Inherited from TemporalAccessor interface
public boolean isSupported(TemporalField field)
Basically, LocalTime supports the following standard fields:
- ChronoField.NANO_OF_SECOND
- ChronoField.NANO_OF_DAY
- ChronoField.MICRO_OF_SECOND
- ChronoField.MICRO_OF_DAY
- ChronoField.MILLI_OF_SECOND
- ChronoField.MILLI_OF_DAY
- ChronoField.SECOND_OF_MINUTE
- ChronoField.SECOND_OF_DAY
- ChronoField.MINUTE_OF_HOUR
- ChronoField.MINUTE_OF_DAY
- ChronoField.HOUR_OF_AMPM
- ChronoField.CLOCK_HOUR_OF_AMPM
- ChronoField.HOUR_OF_DAY
- ChronoField.CLOCK_HOUR_OF_DAY
- ChronoField.AMPM_OF_DAY
Example:
LocalTime_isSupported_ex1.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30.12345"); // 13:45:30.123450000
// boolean isSupported(TemporalField field)
// LocalTime does not support ChronoField.DAY_OF_MONTH, DAY_OF_YEAR,.. (** Used for LocalDateTime, LocalDate)
System.out.println("DAY_OF_MONTH is supported? " + localTime.isSupported(ChronoField.DAY_OF_MONTH)); // false
System.out.println("DAY_OF_YEAR is supported? " + localTime.isSupported(ChronoField.DAY_OF_YEAR)); // false
// LocalTime support ChronoField.MINUTE_OF_DAY, SECOND_OF_MINUTE,..
System.out.println("MINUTE_OF_DAY is supported? " + localTime.isSupported(ChronoField.MINUTE_OF_DAY)); // true
System.out.println("SECOND_OF_MINUTE is supported? " + localTime.isSupported(ChronoField.SECOND_OF_MINUTE)); // true
- TemporalField
- ChronoField
26. isSupported(TemporalUnit)
No ADS
Check if a given TemporalUnit is supported.
// Inherited from Temporal interface
public boolean isSupported(TemporalUnit unit)
Basically, LocalTime supports the following standard units:
- ChronoUnit.NANOS
- ChronoUnit.MICROS
- ChronoUnit.MILLIS
- ChronoUnit.SECONDS
- ChronoUnit.MINUTES
- ChronoUnit.HOURS
- ChronoUnit.HALF_DAYS
Example:
LocalTime_isSupported_ex2.java
// Format: 13:45:30.12345 or 13:45:30 or 13:45
LocalTime localTime = LocalTime.parse("13:45:30.12345"); // 13:45:30.123450000
// boolean isSupported(TemporalUnit unit)
// LocalTime does not support ChronoUnit.CENTURIES, WEEKS,.. (** Used for LocalDateTime, LocalDate)
System.out.println("CENTURIES is supported? " + localTime.isSupported(ChronoUnit.CENTURIES)); // false
System.out.println("WEEKS is supported? " + localTime.isSupported(ChronoUnit.WEEKS)); // false
// LocalTime support ChronoUnit.HOURS, MINUTES,..
System.out.println("HOURS is supported? " + localTime.isSupported(ChronoUnit.HOURS)); // true
System.out.println("MINUTES is supported? " + localTime.isSupported(ChronoUnit.MINUTES)); // true
- TemporalUnit
- ChronoUnit
No ADS
Java Date Time Tutorials
- Java ZoneId Tutorial with Examples
- Java Temporal Tutorial with Examples
- Java Period Tutorial with Examples
- Java TemporalAdjusters Tutorial with Examples
- Java MinguoDate Tutorial with Examples
- Java TemporalAccessor Tutorial with Examples
- Java JapaneseEra Tutorial with Examples
- Java HijrahDate Tutorial with Examples
- Java Date Time Tutorial with Examples
- What is Daylight Saving Time (DST)?
- Java LocalDate Tutorial with Examples
- Java LocalTime Tutorial with Examples
- Java LocalDateTime Tutorial with Examples
- Java ZonedDateTime Tutorial with Examples
- Java JapaneseDate Tutorial with Examples
- Java Duration Tutorial with Examples
- Java TemporalQuery Tutorial with Examples
- Java TemporalAdjuster Tutorial with Examples
- Java ChronoUnit Tutorial with Examples
- Java TemporalQueries Tutorial with Examples
Show More