Skip to content

Getting current date and time with ZonedDateTime

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

java.time.ZonedDateTime is an immutable representation of a date-time with a time-zone.

Getting current date and time with ZonedDateTime

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

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class JavaCurrentDateTimeZonedDateTime {

    public static void main(String[] args) {

        ZonedDateTime now = ZonedDateTime.now();
        
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        System.out.println(dtf.format(now));        
    }
}

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

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