Java LocalDate now() Method Example - Get Current Date and Specific Date

This example shows how to get the current date and specific date using java.time.LocalDate.now() method.

LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. You might use a LocalDate to track a significant event, such as birth date or wedding date.

Read more about LocalDate class with an example at https://www.javaguides.net/2018/08/java-8-localdate-class-api-guide.html.

Java LocalDate now() Method Example - Get Current Date and Specific Date 

LocalDate class provides below APIs to create a current and specific date using LocalDate class.
  • static LocalDate now() - Obtains the current date from the system clock in the default time-zone.
  • static LocalDate now(Clock clock) - Obtains the current date from the specified clock.
  • static LocalDate now(ZoneId zone) - Obtains the current date from the system clock in the specified time-zone.
  • static LocalDate of(int year, int month, int dayOfMonth) - Obtains an instance of LocalDate from a year, month and day.
  • static LocalDate of(int year, Month month, int dayOfMonth) - Obtains an instance of LocalDate from a year, month and day.
import java.time.Clock;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
/**
 * Program to demonstrate LocalDate Class APIs.
 * @author javaguides.net
 *
 */
public class LocalDateExamples {
 
    public static void main(String[] args) {
        createLocalDate();
    }
    private static void createLocalDate() {
        // Current date
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);

        LocalDate localDate1 = LocalDate.now(Clock.systemDefaultZone());
        System.out.println(localDate1);
  
        LocalDate localDate3 = LocalDate.now(Clock.system(ZoneId.of("Indian/Cocos")));
        System.out.println(localDate3);
  
        // Specific date
        LocalDate localDate2 = LocalDate.of(1991, Month.MARCH, 24);
        System.out.println(localDate2);
  
        LocalDate localDate5 = LocalDate.of(1991, 12, 24);
        System.out.println(localDate5);
    }
}
Output:
2018-08-10
2018-08-10
2018-08-10
1991-03-24
1991-12-24

Reference


Comments