LocalTime class represents the time and has got no representation of the date.
It is ideal for representing time on a wall clock such as 10:15:30. For example when we say “Time is ten past three”, it can be represented using ‘LocalTime’ as 3:10:00. LocalTime supports nanosecond precision. The minimum value for LocalTime is midnight (00:00:00.0) at start of the day and maximum value is one nanosecond before midnight(23:59:59.999999999) at end of day.
The “local” part of the name refers to the local time-line.
LocalTime has no reference to a time-zone or offset from UTC/Greenwich.
Creating a LocalTime
There are a number of ways to create a
Creating the current time can be done using the following
The key point is that determining current time requires knowledge of the time-zone.
The first method (a) uses the Java default time-zone, as per
The second method (b) allows the time-zone to be explicitly controlled.
The third method (c) uses a Clock object, which provides further control over the current instant and time-zone. This will come handy in testing scenarios as we can provide an alternate clock.
If you want to hard code the creation of time, or have fields available, these factory methods can be used:
The first one, (a) , creates time from hour and minutes. In this case the seconds and nanoseconds are considered as zero.
The second one, (b), creates time from hour, minutes and seconds. In this case the nanoseconds are considered as zero.
The third one, (c), creates time from hour, minutes, seconds and nanoseconds.
The fourth one, (d), creates time from second of day which is calculated from midnight 00:00.
The fifth one, (e), creates time from nanosecond of day which is calculated from midnight 00:00:00. Remember that nanosecond is billionth of a second. ie 1 second = 1000,000,000 nanoseconds.
Valid value for an hour is 0 - 23 and for minutes/seconds is 0 - 59. If an invalid value is passed in, say 25 as value of hour, 61 as value of minute/second etc, then an exception is thrown.
By Parsing Text
The first twelve methods, (a) to (l), creates a
LocalTime from text of the format hh:mm:ss.SSSSSSSSS. Second and nano-of-second parts of text are optional and you can safely omit the trailing zeros of nano-of-second. For example 14:30:40.001 denotes 14:30:40.001000000 .
The final method (m) uses an additional
DateTimeFormatter which specifies the format of text used.
When using toString() method of
LocalTime, remember that it prints either 0, 3, 6 or 9 digits for nano-of-second field, depending on the value. So for example 14:30:40.100000000 will be printed as 14:30:40.100 .