LocalTime
suggest changeTo use just the time of day, use LocalTime. You can instantiate a LocalTime object in several ways:
LocalTime time = LocalTime.now();time = LocalTime.MIDNIGHT;time = LocalTime.NOON;time = LocalTime.of(12, 12, 45);
LocalTime also has a built-in toString method that displays the format very nicely.
System.out.println(time);
You can also get, add, and subtract hours, minutes, seconds, and nanoseconds. plusMinutes and minusMinutes return a new LocalTime; these calls do not change time:
time.plusMinutes(1);
time.getMinutes();
time.minusMinutes(1);
You can turn it into a Date object with the following code:
LocalTime lTime = LocalTime.now();
Instant instant = lTime.atDate(LocalDate.of(A_YEAR, A_MONTH, A_DAY)).
atZone(ZoneId.systemDefault()).toInstant();
Date time = Date.from(instant);
This class works very nicely within a timer class to simulate an alarm clock.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents