Open In App

OffsetDateTime now() method in Java with Example

Improve
Improve
Like Article
Like
Save
Share
Report
  1. 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
    
  2. 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
    
  3. 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
    

References:



Last Updated : 05 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads