OffsetDateTime of() method in Java with Examples

The of(int year, int month, int day, int hour, int minute, int second, int nanosecond, ZoneOffset offset) method of the OffsetDateTime class in Java is used to create an instance of OffsetDateTime from the passed values of year, month, day, hour, minute, second, nanosecond and offset. In this method, the value of the year, month, day, hour, minute, second and nano-second is passed in integer format and the method returns date-time on the basis of these values.

Syntax:

public static OffsetDateTime of(int year,
                                int month,
                                int day,
                                int hour,
                                int minute,
                                int second,
                                int nanosecond,
                                ZoneOffset offset)

Parameters: This method accepts eight parameters.

  • year – It is of Integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
  • month – It is of Integer type and represents the month of the year. It varies from 1(JANUARY) to 12(DECEMBER).
  • day – It is of Integer type and represents the day of the month. It varies from 1 to 31.
  • hour – It is of Integer type and represents the hour of the day. It varies from 0 to 23.
  • minute – It is of Integer type and represents the minute of the hour. It varies from 0 to 59.
  • second – It is of Integer type and represents the second of the minute. It varies from 0 to 59.
  • nanosecond – It is of Integer type and represents the nanosecond. It varies from 0 to 999999999.
  • offset – It is of ZoneOffset type and represents the zone offset. It should not be null.

Return value: This method returns the OffsetDateTime.

Exception: This method throws DateTimeException if any field value is out of range or the day of the month is invalid for the month-year.



Below programs illustrate the of() method of OffsetDateTime class in Java:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// OffsetDateTime of() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(
        String[] args)
    {
        // Create OffsetDateTime object
        OffsetDateTime offsetdatetime
            = OffsetDateTime.of(
                2020, 5, 20, 9, 10, 40,
                50000, ZoneOffset.UTC);
  
        // Print date-time
        System.out.println(
            "DATE-TIME: "
            + offsetdatetime);
    }
}

chevron_right


Output:

DATE-TIME: 2020-05-20T09:10:40.000050Z

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// OffsetDateTime of() method
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create OffsetDateTime object
        OffsetDateTime offsetdatetime
            = OffsetDateTime.of(
                2020, 5, 20, 9, 10, 40,
                20000, ZoneOffset.ofHoursMinutes(
                           5, 30));
  
        // Print date-time
        System.out.println(
            "DATE-TIME: "
            + offsetdatetime);
    }
}

chevron_right


Output:

DATE-TIME: 2020-05-20T09:10:40.000020+05:30

References:
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#of(int, int, int, int, int, int, int, java.time.ZoneOffset)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.