Java LocalTime of() Method
Example
Create two fixed times:
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime time1 = LocalTime.of(14, 30);
LocalTime time2 = LocalTime.of(9, 5, 45);
System.out.println(time1);
System.out.println(time2);
}
}
Definition and Usage
The of() method creates a LocalTime object from an hour and a minute, and optionally a second and a nanosecond (a billionth of a second).
The hour uses the 24-hour clock, from 0 to 23. When you print the time, the seconds are left out if they are zero.
Syntax
One of the following:
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)
Parameter Values
| Parameter | Description |
|---|---|
| hour | Required. The hour, from 0 to 23. |
| minute | Required. The minute, from 0 to 59. |
| second | Optional. The second, from 0 to 59. |
| nanoOfSecond | Optional. The nanosecond, from 0 to 999999999. |
Technical Details
| Returns: | A LocalTime object with the given time. |
|---|---|
| Throws: | DateTimeException - if a value is out of range, for example the hour 24. |
Related Pages
❮ Date and Time Methods