Java Language LocalTime


Example

To use just the time part of a Date use LocalTime. You can instantiate a LocalTime object in a couple ways

  1. LocalTime time = LocalTime.now();
  2. time = LocalTime.MIDNIGHT;
  3. time = LocalTime.NOON;
  4. 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 from the LocalTime object i.e.

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.