Along with lambda expression, virtual methods, streams and many other nice features, Java 8 has also updated a new Date and Time API which is maintained under the JSR-310. One of the greatest on this new API is that all the date and time related APIs are unified under the same package java.time. In my previous article I have explained about the LocalDate.This example demonstrates how to use one of the new class java.time.LocalTime defined under that date and time package.
If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.
LocalTime
A LocalTime object is an immutable class that represents just a time. The time is normally stored as hours-minutes-seconds. The time is stored in the nanoseconds. For example, the value “11:46.20.4567345” can be stored in a LocalTime object. LocalTime does not store or represent date or time zone. It is just local time seen on the wall clock.
This class defines several methods that are useful for fetching the time related values from the stored object. This example represents few important methods are useful for the programmers in normal scenarios.
- Class Name : LocalTime
- Package : java.time
- Version Released : Java 1.8
LocalTime – Few Methods
- now() – This static method gets the current time from system clock with the default time zone.
- now(ZoneId zone) – This static method gets the current time from the system clock with the specified time zone.
- getHour() – This static method gets the hour field in the current day
- getMinute() – This static method gets the minute field in the current hour
- getSecond() – This static method gets the second field in the current minute
- of(int hour, int minute, int second) – This static method retruns LocalTime instance for the given hour,minute and second
- isAfter(LocalTime other) – This instance method checks whether the specified time is after the time stored in the LocalTime object
- plusHours(long hoursToAdd) – This instance method returns the new LocalTime instance by adding the specified hours to the LocalTime object
LocalTime – Example
package javabeat.net; import java.time.LocalTime; import java.time.ZoneId; /** * LocalTime Example * * @author krishna (www.javabeat.net) * */ public class LocalTimeExample { public static void main(String[] args) { LocalTime localtimeToday = LocalTime.now(); LocalTime localTimeZone = LocalTime.now(ZoneId .of("America/Los_Angeles")); int hour = localtimeToday.getHour(); int minute = localtimeToday.getMinute(); int second = localtimeToday.getSecond(); LocalTime ofMethod = LocalTime.of(10,10,10); boolean isAfterMethod = localtimeToday.isAfter(ofMethod); LocalTime plusHoursMethod = localtimeToday.plusHours(10); System.out.println("LocalTime Example Demo"); System.out.println("----------------------"); System.out.println("Current Time : " + localtimeToday); System.out.println("Current Time at Zone America/Los_Angeles : " + localTimeZone); System.out.println("Hour : " + hour); System.out.println("Minute : " + minute); System.out.println("Second : " + second); System.out.println("Time.of() : " + ofMethod); System.out.println("LocalDate().isAfter() : " + isAfterMethod); System.out.println("LocalDate().isAfter() : " + isAfterMethod); System.out.println("LocalDate().plusHoursMethod() : " + plusHoursMethod); } }
Output for the above program will be:
LocalTime Example Demo ---------------------- Current Time : 07:17:42.435 Current Time at Zone America/Los_Angeles : 18:47:42.443 Hour : 7 Minute : 17 Second : 42 Time.of() : 10:10:10 LocalDate().isAfter() : false LocalDate().isAfter() : false LocalDate().plusHoursMethod() : 17:17:42.435
If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.