Date and Time

Java get current date without time using LocalDate Java 8 API

LocalDate API has an improved way of working with date, many developers struggle to get a current date without time, java 8 made it simple with its factory method now(). Now

Obtains the current date from the system clock in the default time-zone.

LocalDate.now contains three overloaded methods

LocalDate.now();

Gets the current system date without time

LocalDate.now(Clock.systemDefaultZone());

Gets the current system date based on its system default zone id

LocalDate.now(ZoneId.of(“Europe/Paris”));

Converts the current system date to zone id and returns its date as LocalDate

Example:

import java.time.LocalDate;

public class LocalDateTest 
{
 public static void main(String[] args) 
 {
 
 LocalDate currentDate= LocalDate.now();
 
 LocalDate currentDateBySystemTZ=LocalDate.now(Clock.systemDefaultZone());
 
 LocalDate currentDateByTZ=LocalDate.now(ZoneId.of("Europe/Paris"));

 System.out.println("Current date : "+currentDate);
 
 System.out.println("Current date by system time zone : "+ currentDateBySystemTZ);
 
 System.out.println("Current date by Europe/Paris time zone : "+ currentDateByTZ);
 
 
 }
}

Output:

Current date : 2020-02-03
Current date by system time zone : 2020-02-03
Current date by Europe/Paris time zone : 2020-02-03