JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Java 8 Date Time API – LocalDateTime Example

April 14, 2015 by Krishna Srinivasan Leave a Comment

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

[code lang=”java”]
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);
}
}

[/code]

Output for the above program will be:

[code]
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
[/code]

Filed Under: Java Tagged With: Java 8

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved