Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaLocalDate now() Method in Java with Examples

LocalDate now() Method in Java with Examples

In LocalDate class, there are three types of now() method depending upon the parameters passed to it.

now()

now() method of a LocalDate class used to obtain the current date from the system clock in the default time-zone.This method will return LocalDate based on system clock with default time-zone to obtain the current date.

Syntax:

public static LocalDate now()

Parameters: This method accepts no parameter.

Return value: This method returns the current date using the system clock and default time-zone.

Below programs illustrate the now() method:
Program 1:




// Java program to demonstrate
// LocalDate.now() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an LocalDate object
        LocalDate lt
            = LocalDate.now();
  
        // print result
        System.out.println("LocalDate : "
                           + lt);
    }
}


Output:

LocalDate : 2019-01-21

now(Clock clock)

now(Clock clock) method of a LocalDate class used to return the current date based on the specified clock passed as parameter.

Syntax:

public static LocalDate now(Clock clock)

Parameters: This method accepts clock as parameter which is the clock to use.

Return value: This method returns the current date.

Below programs illustrate the now() method:
Program 1:




// Java program to demonstrate
// LocalDate.now() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a clock
        Clock cl = Clock.systemUTC();
  
        // create an LocalDate object using now(Clock)
        LocalDate lt
            = LocalDate.now(cl);
  
        // print result
        System.out.println("LocalDate : "
                           + lt);
    }
}


Output:

LocalDate : 2019-01-21

now(ZoneId zone)

now(ZoneId zone) method of a LocalDate class used to return the current date from system clock in the specified time-zone passed as parameter.Specifying the time-zone avoids dependence on the default time-zone.

Syntax:

public static LocalDate now(ZoneId zone)

Parameters: This method accepts zone as parameter which is the zone to use.

Return value: This method returns the current date-time.

Below programs illustrate the now() method:
Program 1:




// Java program to demonstrate
// LocalDate.now() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a clock
        ZoneId zid = ZoneId.of("Europe/Paris");
  
        // create an LocalDate object using now(zoneId)
        LocalDate lt
            = LocalDate.now(zid);
  
        // print result
        System.out.println("LocalDate : "
                           + lt);
    }
}


Output:

LocalDate : 2019-01-21

References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#now()
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#now(java.time.Clock)
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#now(java.time.ZoneId)

RELATED ARTICLES

Most Popular

Recent Comments