Java 8 LocalDateTime class

The java.time.LocalDateTime class is an immutable date-time class that represents a date-time with the default format as yyyy-MM-dd-HH-mm-ss.zzz. This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.

Java LocalDateTime class methods

String format(DateTimeFormatter formatter)It is used to format this date-time using the specified formatter.
int get(TemporalField field)It is used to get the value of the specified field from this date-time as an int.
LocalDateTime minusDays(long days)It is used to return a copy of this LocalDateTime with the specified number of days subtracted.
static LocalDateTime now()It is used to obtain the current date-time from the system clock in the default time-zone.
static LocalDateTime of(LocalDate date, LocalTime time)It is used to obtain an instance of LocalDateTime from a date and time.
LocalDateTime plusDays(long days)It is used to return a copy of this LocalDateTime with the specified number of days added.
boolean equals(Object obj)It is used to check if this date-time is equal to another date-time.


package com.w3spoint;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
public class TestExample {
	public static void main(String args[]){
		LocalDateTime now =;  
        System.out.println("Before Formatting: " + now);  
        DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
        String formatDateTime = now.format(format);  
        System.out.println("After Formatting: " + formatDateTime);  
        //Get different values
        LocalDateTime dateTime1 = LocalDateTime.of(2018, 3, 23, 22, 46);    
        //Minus days
        LocalDateTime dateTime2 = LocalDateTime.of(2018, 2, 23, 22, 46);   
        LocalDateTime dateTime3 = dateTime2.minusDays(100);  
        System.out.println("Before Formatting: " + dateTime3);  
        formatDateTime = dateTime3.format(format);   
        System.out.println("After Formatting: " + formatDateTime ); 
        //Plus days
        LocalDateTime dateTime4 = LocalDateTime.of(2018, 5, 23, 22, 46);   
        LocalDateTime dateTime5 = dateTime4.plusDays(100);  
        System.out.println("Before Formatting: " + dateTime5);  
        formatDateTime = dateTime5.format(format);   
        System.out.println("After Formatting: " + formatDateTime ); 


Before Formatting: 2018-04-15T18:45:45.318
After Formatting: 15-04-2018 18:45:45
Before Formatting: 2017-11-15T22:46
After Formatting: 15-11-2017 22:46:00
Before Formatting: 2018-08-31T22:46
After Formatting: 31-08-2018 22:46:00
Content Protection by
Please Share