Posts

Showing posts with the label Java8DateTimeAPI

Java 8 LocalTime class in with code samples

What is LocalTime class? The Java 8 LocalTime class represents a time. It has the hour, minute, second and nanoseconds components. So for example, you can use it to represent a time like 7:30. It does not have a date, year or month component. Edit LocalTime Creation There are several static methods on this class that can be used to create a LocalTime object. The following code demonstrates this: LocalTime time1 = LocalTime.now();System.out.println("Current time is "+time1);LocalTime time2 = LocalTime.parse("12:30");System.out.println("time2 is "+time2);LocalTime time3 = LocalTime.of(5, 30);System.out.println("time3 is "+time3);   The LocalTime.now returns the current time. The LocalTime.parse parses the specified time and creates a LocalTime object out of it. It requires the Time to be in hh-mm format. If you specify the time in some other format, an error will occur. If your String time is in any other format, you can use the overloaded ve...