Java Reference
In-Depth Information
LocalTime time = LocalTime.now();
System.out.println("Current Time: " + time);
Once the time has been obtained, methods can be called against the LocalTime
instance to achieve the desired result. In the following lines of code, there are some ex-
amples of using the LocalTime methods:
// atDate(LocalDate): obtain the local date and time
LocalDateTime ldt
= time.atDate(LocalDate.of(2011,Month.NOVEMBER,11));
System.out.println("Local Date Time object: " + ldt);
// of(int hours, int min): obtain a specific time
LocalTime pastTime = LocalTime.of(1, 10);
// compareTo(LocalTime): compare two times. Positive
// return value returned if greater
System.out.println("Comparing times: "
+ time.compareTo(pastTime));
// getHour(): return hour in int value (24-hour format)
int hour = time.getHour();
System.out.println("Hour: " + hour);
// isAfter(LocalTime): return Boolean comparison
System.out.println("Is local time after pastTime? "
+ time.isAfter(pastTime));
// minusHours(int): Subtract Hours from LocalTime
LocalTime minusHrs = time.minusHours(5);
System.out.println("Time minus 5 hours: " + minusHrs);
// plusMinutes(int): Add minutes to LocalTime
LocalTime plusMins = time.plusMinutes(30);
System.out.println("Time plus 30 mins: " + plusMins);
Here are the results:
Search WWH ::




Custom Search