Enjoy with LocalDateTime in Java 8
Posted By : Keshav Agarawal | 29-Jul-2019
LocalDateTime class is an immutable class. It is thread safe.
This class provides output in the form of date and time such as 20019-07-29T10:15:30.
We use LocalDateTime instance for general-purpose date and time information. It has no time zone data.
How to import this class?
This class is available in java.time package.
By writing this statement "import java.time.LocalDateTime;" we can import a class
How to create an instance of LocalDateTime class?
1. By invoking a static method in LocalDateTime class i.e. now() method
LocalDateTime ldt = LocalDateTime.now();
2. Bypassing year, month, day, hour, minute, second and nanosecond values to of() method. This of() method is also a static method.
LocalDateTime ldt = LocalDateTime.of(2019,07,29,6,30,40,50000);
This method is overloaded and one of them takes argument at LocalDate and LocalTime.
LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.now());
3. By passing date time string representation as an argument i.e.
LocalDateTime ldt = LocalDateTime.parse("2019-07-29T22:11:03.460446");
How to Retrieving Date Time Information from LocalDateTime?
1. int getHour(): this method returns the hour contained within the LocalDateTime object, from 0 to 23.
2. int getMinute(): this method returns the minute information, from 0 to 59.
3. int getSecond(): this method returns the second contained within the LocalDateTime object, from 0 to 59.
4. int getNano(): this method returns the nanosecond from the LocalDateTime object, ranging from 0 to 999,999,999.
This LocalDateTime class has different methods to plus and minus date time components. These methods return a new instance if LocalDateTime. But do not perform any changes in the existing instance as this class is immutable.
Let's see these methods:
1. plusHours(), plusMinutes(), plusSeconds(), plusNanos() : these methods adding time component in LocalDateTime object.
2. minusHours(), minusMinutes(), minusSeconds() and minusNanos(): these methods subtracting time component in LocalDateTime object.
3. plusYears(), plusMonths(), plusWeeks(), plusDays() : these methods adding date component in LocalDateTime object.
4. minusYears(), minusMonths(), minusWeeks(), minusDays() : these methods subtracting date component in LocalDateTime object.
As the name of these methods itself suggests that what these methods are used for. All the methods take a long type of argument and return a new instance of LocalDateTime.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Keshav Agarawal
Keshav Agarawal is Java Developer with skilled in Java, Servlet, JSP, JDBC, Spring Boot, Spring MVC, JPA, Hibernate. His hobbies are playing chess and reading books in free time. By nature he is friendly and honest person.