LocalTime
layout: default
title: LocalTime
LocalTime represents a 24-hour wall-clock time, encapsulating hour, minute, and second - with nanosecond precision.
- No date or time-zone information is stored.
The static of method is overloaded, allowing callers to pass:
- Just the hour and minute
- The hour, minute, and second
- Hour, minute, second, and nanonsecond
LocalTime oneThirty = LocalTime.of(13, 30);
Like LocalDate, use the now method to get the current time.
LocalTime rightNow = LocalTime.now();
System.out.println(rightNow);
// "14:20:55.939"
Both LocalDate and LocalTime are immutable; any method you call that seems to modify the object actually builds a new instance and returns it to you.
LocalTime twoThirty = oneThirty.plusHours(1);
System.out.println(oneThirty);
// "13:30"
System.out.println(twoThirty);
// "14:30"
Drill¶
DatesAndTimes/com.example.datesandtimes.drills.TimeDrillIn
main: * Create aLocalDateand aLocalTimerepresenting the current date and time. * Print both out. * Use theLocalTimeabove to create aLocalTimefor 90 minutes in the future, and print it out. (Refer to theLocalTimeJavaDoc for methods: https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html) * What happens if you use your originalLocalTimeto create aLocalTimefor 22 hours in the future?(Solution:
DatesAndTimes/com.example.datesandtimes.solutions.drills.TimeDrill)