Skip to content

Getting current date and time with LocalDateTime

Ramesh Fadatare edited this page Jul 11, 2019 · 1 revision

java.time.LocalDateTime creates a date-time without a time-zone.

Getting current date and time with LocalDateTime

The example uses java.time.LocalDateTime to get the current date time and formats it with java.time.format.DateTimeFormatter.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JavaCurrentDateLocalDateTime {

    public static void main(String[] args) {
        
        LocalDateTime now = LocalDateTime.now();
        
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        System.out.println(dtf.format(now));
    }
}

The LocalDateTime.now() method obtains the current date-time from the system clock in the default time-zone:

LocalDateTime now = LocalDateTime.now();
Clone this wiki locally