OffsetDateTime now() method in Java with Example - GeeksforGeeks

05 Jun, 2020
The now() method of the OffsetDateTime class in Java is used to obtain the current offset date-time using the system clock. This is done after querying the system clock in the default time zone. The method uses the hardcoded clock for testing instead of the alternative clock.Syntax:public static OffsetDateTime now()Parameters: The method does not accept any parameter.Return value: The method returns the current OffsetDateTime using the system clock. It does not return Null.Exception: This method does not throw any exception.Below program illustrates the now() method of OffsetDateTime class in Java:Program:// Java program to demonstrate// OffsetDateTime now() method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now();          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}Output:DATE-TIME: 2020-05-20T05:40:08.721Z
The now(Clock clock) method of the OffsetDateTime class in Java is used to get the current date-time using a specified clock. This is done after querying the specified clock while the offset is calculated using the timezone in the clock. As already specified this method can use an alternative clock, unlike the previously mentioned now() method.Syntax:public static OffsetDateTime now(Clock clock)
Parameters: This method accepts clock of Clock type which is used to get the required date-time.Return value: This method returns the current OffsetDateTime using the specified clock.Exception: This method does not throw any exception.Below program illustrates the now(Clock clock) method of OffsetDateTime class in Java:Program:// Java program to demonstrate// OffsetDateTime now(Clock clock) method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now(                Clock.systemUTC());          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}Output:DATE-TIME: 2020-05-20T13:12:18.825Z
The now(ZoneId zone) method of the OffsetDateTime class in Java is used to obtain the current date-time by using the system clock in the specified time zone. This is done after querying the system clock in the specified time zone. The method uses the hardcoded clock for testing instead of the alternative clock.Syntax:public static OffsetDateTime now(ZoneId zone)
Parameters: This method accepts zone as a parameter. It is used to get the date-time.Return value: This method returns the current OffsetDateTime using the system clock in the specified time zone.Exception: This method does not throw any exception.Below program illustrates the now(ZoneID zone) method of OffsetDateTime class in Java:Program:// Java program to demonstrate// OffsetDateTime now(ZoneId zone) method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now(                ZoneId.systemDefault());          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}Output:DATE-TIME: 2020-05-20T13:12:40.458Z

The now() method of the OffsetDateTime class in Java is used to obtain the current offset date-time using the system clock. This is done after querying the system clock in the default time zone. The method uses the hardcoded clock for testing instead of the alternative clock.Syntax:public static OffsetDateTime now()Parameters: The method does not accept any parameter.Return value: The method returns the current OffsetDateTime using the system clock. It does not return Null.Exception: This method does not throw any exception.Below program illustrates the now() method of OffsetDateTime class in Java:Program:// Java program to demonstrate// OffsetDateTime now() method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now();          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}Output:DATE-TIME: 2020-05-20T05:40:08.721Z

Syntax:
public static OffsetDateTime now()
Parameters: The method does not accept any parameter.
Return value: The method returns the current OffsetDateTime using the system clock. It does not return Null.
Exception: This method does not throw any exception.
Below program illustrates the now() method of OffsetDateTime class in Java:Program:
// Java program to demonstrate// OffsetDateTime now() method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now();          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}
DATE-TIME: 2020-05-20T05:40:08.721Z

The now(Clock clock) method of the OffsetDateTime class in Java is used to get the current date-time using a specified clock. This is done after querying the specified clock while the offset is calculated using the timezone in the clock. As already specified this method can use an alternative clock, unlike the previously mentioned now() method.Syntax:public static OffsetDateTime now(Clock clock)
Parameters: This method accepts clock of Clock type which is used to get the required date-time.Return value: This method returns the current OffsetDateTime using the specified clock.Exception: This method does not throw any exception.Below program illustrates the now(Clock clock) method of OffsetDateTime class in Java:Program:// Java program to demonstrate// OffsetDateTime now(Clock clock) method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now(                Clock.systemUTC());          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}Output:DATE-TIME: 2020-05-20T13:12:18.825Z

Syntax:
public static OffsetDateTime now(Clock clock)

Parameters: This method accepts clock of Clock type which is used to get the required date-time.
Return value: This method returns the current OffsetDateTime using the specified clock.
Exception: This method does not throw any exception.
Below program illustrates the now(Clock clock) method of OffsetDateTime class in Java:
Program:
// Java program to demonstrate// OffsetDateTime now(Clock clock) method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now(                Clock.systemUTC());          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}
DATE-TIME: 2020-05-20T13:12:18.825Z

The now(ZoneId zone) method of the OffsetDateTime class in Java is used to obtain the current date-time by using the system clock in the specified time zone. This is done after querying the system clock in the specified time zone. The method uses the hardcoded clock for testing instead of the alternative clock.Syntax:public static OffsetDateTime now(ZoneId zone)
Parameters: This method accepts zone as a parameter. It is used to get the date-time.Return value: This method returns the current OffsetDateTime using the system clock in the specified time zone.Exception: This method does not throw any exception.Below program illustrates the now(ZoneID zone) method of OffsetDateTime class in Java:Program:// Java program to demonstrate// OffsetDateTime now(ZoneId zone) method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now(                ZoneId.systemDefault());          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}Output:DATE-TIME: 2020-05-20T13:12:40.458Z

Syntax:
public static OffsetDateTime now(ZoneId zone)

Parameters: This method accepts zone as a parameter. It is used to get the date-time.
Return value: This method returns the current OffsetDateTime using the system clock in the specified time zone.
Exception: This method does not throw any exception.
Below program illustrates the now(ZoneID zone) method of OffsetDateTime class in Java:
Program:
// Java program to demonstrate// OffsetDateTime now(ZoneId zone) method  import java.time.*;import java.time.temporal.*;  public class GFG {    public static void main(String[] args)    {        // Create OffsetDateTime object        OffsetDateTime offsetdatetime            = OffsetDateTime.now(                ZoneId.systemDefault());          // Print date-time        System.out.println(            "DATE-TIME: "            + offsetdatetime);    }}
DATE-TIME: 2020-05-20T13:12:40.458Z

References:
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#now()
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#now(java.time.Clock)
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#now(java.time.ZoneId)
Java-Functions
Java-OffsetDateTime
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Functional Interfaces in Java
Generics in Java
Comparator Interface in Java with Examples
Introduction to Java
HashMap get() Method in Java
Strings in Java