LocalDateTime now() Method in Java with Examples - GeeksforGeeks

21 Jan, 2019
In LocalDateTime class, there are three types of now() method depending upon the parameters passed to it.
now() method of a LocalDateTime class used to obtain the current date-time from the system clock in the default time-zone.This method will return LocalDateTime based on system clock with default time-zone to obtain the current date-time.
Syntax:
public static LocalDateTime now()

Parameters: This method accepts no parameter.
Return value: This method returns the current date-time using the system clock.
Below programs illustrate the now() method:Program 1:
// Java program to demonstrate// LocalDateTime.now() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // create an LocalDateTime object        LocalDateTime lt            = LocalDateTime.now();          // print result        System.out.println("LocalDateTime : "                           + lt);    }}
LocalDateTime : 2019-01-21T05:47:08.644

now(Clock clock) method of a LocalDateTime class used to return the current date-time based on the specified clock passed as parameter
Syntax:
public static LocalDateTime now(Clock clock)

Parameters: This method accepts clock as parameter which is the clock to use.
Return value: This method returns the current date-time.
Below programs illustrate the now() method:Program 1:
// Java program to demonstrate// LocalDateTime.now() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // create a clock        Clock cl = Clock.systemUTC();          // create an LocalDateTime object using now(Clock)        LocalDateTime lt            = LocalDateTime.now(cl);          // print result        System.out.println("LocalDateTime : "                           + lt);    }}
LocalDateTime : 2019-01-21T05:47:20.949

now(ZoneId zone) method of a LocalDateTime class used to return the current date-time 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 LocalDateTime 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// LocalDateTime.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 LocalDateTime object using now(zoneId)        LocalDateTime lt            = LocalDateTime.now(zid);          // print result        System.out.println("LocalDateTime : "                           + lt);    }}
LocalDateTime : 2019-01-21T06:47:22.756

References:https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#now()https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#now(java.time.Clock)https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#now(java.time.ZoneId)
Java-Functions
Java-LocalDateTime
Java-time package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Initialize an ArrayList in Java
Interfaces in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
LinkedList in Java
Collections in Java
Set in Java
Overriding in Java