Saturday, October 11, 2025
HomeLanguagesJavaOffsetDateTime now() method in Java with Example

OffsetDateTime now() method in Java with Example

  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:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32351 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7102 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS