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 articles I have explained about the LocalDate , LocalTime and few other APIs introduced with the Java 8 version.This example demonstrates how to use LocalDateTime class with few important methods defined in that class.
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.
LocalDateTime
java.time.LocalDateTime is an immutable, thread safe object which represents date-time without a time-zone in the ISO-8601 calendar system, such as 2014-03-30T02:51:21. This class normally stores date-time in year-month-day-hour-minute-second and provides accuracy up to the range of nanoseconds.
- Class Name : LocalDateTime
- Package : java.time
- Version Released : Java 1.8
LocalDateTime – Few Methods
- now() – This static method returns the current date time in the default system time zone
- now(ZoneId zoneId) – This static method returns the current date time in the specified time zone
- of(LocalDate,LocalTime) – This static method returns date time for the specified LocatDate and LocalTime objects
- of(year,month,day,hour,miniute,second,nanoseconds) – This static method returns date time by taking the arguments year, month, day, hour, minutes, seconds and nanoseconds
- of(year,month in enum,day,hour,miniute) – This static method returns date time by taking the arguments year, month in enum format, day, hour, minutes
- getDayOfMonth() – This is an instance method to return the current stored value of day
- getHour() – This is an instance method to return the current stored value of hour
- getMonth() – This is an instance method to return the current stored value of month
- plusDays(int) – This is an instance method to return the date time by adding the number is days passed to the method
- minusDays(int) – This is an instance method to return the date time by reducing the number is days passed to the method
- toLocalDate() – This is an instance method to return the LocalDate from the current LocalDateTime object
- toLocalTime() – This is an instance method to return the LocalTime from the current LocalDateTime object
LocalDateTime – Example
package javabeat.net; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; import java.time.ZoneId; public class LocalDateTimeExample { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTimeZone = LocalDateTime.now(ZoneId .of("America/Los_Angeles")); LocalDateTime today = LocalDateTime .of(LocalDate.now(), LocalTime.now()); LocalDateTime ofMethod = LocalDateTime.of(2015, 4, 14, 12, 30, 23, 12000); LocalDateTime ofMethod1 = LocalDateTime .of(2015, Month.APRIL, 1, 12, 30); int dayOfMonth = ofMethod1.getDayOfMonth(); int hour = ofMethod1.getHour(); Month month = ofMethod1.getMonth(); LocalDateTime plusDays = ofMethod1.plusDays(10); LocalDateTime minusDays = ofMethod1.minusDays(12); LocalDate localDate = ofMethod1.toLocalDate(); LocalTime localTime = ofMethod1.toLocalTime(); System.out.println("LocalDateTime Example Demo"); System.out.println("--------------------------"); System.out.println("Current Date Time : " + localDateTime); System.out.println("Current Date Time at America/Los_Angeles : " + localDateTimeZone); System.out.println("Current DateTime=" + today); System.out .println("LocalDateTime.0f(year, month,day, hour, minute, seconds, nano seconds) is :" + ofMethod); System.out .println("LocalDateTime.0f(year, month IN ENUM,day, hour, minute) is :" + ofMethod1); System.out.println("LocalDateTime().getDayOfMonth : " + dayOfMonth); System.out.println("LocalDateTime().getHour : " + hour); System.out.println("LocalDateTime().getMonth : " + month); System.out.println("LocalDateTime().plusDays : " + plusDays); System.out.println("LocalDateTime().minusDays : " + minusDays); System.out.println("LocalDateTime().toLocalDate : " + localDate); System.out.println("LocalDateTime().toLocalTime : " + localTime); } }
Output for the above program will be:
LocalDateTime Example Demo -------------------------- Current Date Time : 2015-04-14T06:45:44.323 Current Date Time at America/Los_Angeles : 2015-04-13T18:15:44.325 Current DateTime=2015-04-14T06:45:44.333 LocalDateTime.0f(year, month,day, hour, minute, seconds, nano seconds) is :2015-04-14T12:30:23.000012 LocalDateTime.0f(year, month IN ENUM,day, hour, minute) is :2015-04-01T12:30 LocalDateTime().getDayOfMonth : 1 LocalDateTime().getHour : 12 LocalDateTime().getMonth : APRIL LocalDateTime().plusDays : 2015-04-11T12:30 LocalDateTime().minusDays : 2015-03-20T12:30 LocalDateTime().toLocalDate : 2015-04-01 LocalDateTime().toLocalTime : 12:30