In ZonedDateTime class, there are three types of now() method depending upon the parameters passed to it.
now()
now() method of a ZonedDateTime class used to obtain the current date-time from the system clock in the default time-zone.This method will return ZonedDateTime based on system clock with default time-zone to obtain the current date-time. The zone and offset will be set based on the time-zone in the clock.
Syntax:
public static ZonedDateTime 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 // ZonedDateTime.now() method import java.time.*; public class GFG { public static void main(String[] args) { // create an ZonedDateTime object ZonedDateTime lt = ZonedDateTime.now(); // print result System.out.println( "ZonedDateTime : " + lt); } } |
ZonedDateTime : 2019-01-21T05:36:18.973Z[Etc/UTC]
now(Clock clock)
now(Clock clock) method of a ZonedDateTime class used to return the current date-time based on the specified clock passed as parameter.The zone and offset will be set based on the time-zone in the clock.
Syntax:
public static ZonedDateTime 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 // ZonedDateTime.now() method import java.time.*; public class GFG { public static void main(String[] args) { // create a clock Clock cl = Clock.systemUTC(); // create an ZonedDateTime object using now(Clock) ZonedDateTime lt = ZonedDateTime.now(cl); // print result System.out.println( "ZonedDateTime : " + lt); } } |
ZonedDateTime : 2019-01-21T05:36:25.966Z
now(ZoneId zone)
now(ZoneId zone) method of a ZonedDateTime 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. The offset will be calculated from the specified time-zone.
Syntax:
public static ZonedDateTime 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 // ZonedDateTime.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 ZonedDateTime object using now(zoneId) ZonedDateTime lt = ZonedDateTime.now(zid); // print result System.out.println( "ZonedDateTime : " + lt); } } |
ZonedDateTime : 2019-01-21T06:36:30.188+01:00[Europe/Paris]
References:
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#now()
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#now(java.time.Clock)
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#now(java.time.ZoneId)