Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaOffsetDateTime of() method in Java with Examples

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:




// 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);
    }
}


Output:

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

Program 2:




// 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);
    }
}


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)

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

Most Popular

Recent Comments