How to get the current date in java

Syntax:


LocalDate ld = LocalDate.now();

In java, the current Date can be accessed using the method now of the LocalDate object.

Example:

The example first creates a localDate object calling the now method of the LocaleDate object. That date is converted to a String using the format method and the ISO_DATE formatter that is part of the DateTimeFormatter Object. The String is then written in the output.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class CurrentDateTest {

    public static void main(String[] argv) {
	LocalDate ld = LocalDate.now();

	System.out.println("Date in ISO format: " + ld.format(DateTimeFormatter.ISO_DATE));
    }
}

The output will be:

Date in ISO format: 2018-08-13

References:

Java LocalDate

Share this article:

Recent Comments