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

Method Description
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.

Example

package com.w3schools;
 
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 = 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);    
        System.out.println(dateTime1.get(ChronoField.DAY_OF_WEEK));  
        System.out.println(dateTime1.get(ChronoField.DAY_OF_YEAR));  
        System.out.println(dateTime1.get(ChronoField.DAY_OF_MONTH));  
        System.out.println(dateTime1.get(ChronoField.HOUR_OF_DAY));  
        System.out.println(dateTime1.get(ChronoField.MINUTE_OF_DAY));   
 
        //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 ); 
 
	}  
}

Output

Before Formatting: 2018-04-15T18:45:45.318
After Formatting: 15-04-2018 18:45:45
5
82
23
22
1366
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