OffsetDateTime of(LocalDateTime) method in Java with Examples

The of(LocalDateTime dateTime, ZoneOffset offset) method of the OffsetDateTime class in Java is used to create an instance of OffsetDateTime from given instances of date-time and offset. This method creates an OffsetDateTime with the specified local date-time and offset.

Syntax:

public static OffsetDateTime
       of(LocalDateTime dateTime,
          ZoneOffset offset)

Parameters: This method accepts two parameters:

  • dateTime – It represents the local date-time.
  • offset – It represents the zone offset.

Return value: This method returns the OffsetDateTime.

Exception: This method does not throw any exception.



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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// OffsetDateTime of(LocalDateTime,
// ZoneOffset) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Create LocalDateTime object
        LocalDateTime dateTime
            = LocalDateTime.parse(
                "2020-05-28T04:12:50");
  
        // Create OffsetDateTime object
        OffsetDateTime offsetdatetime
            = OffsetDateTime.of(
                dateTime, ZoneOffset.UTC);
  
        // Print date-time
        System.out.println("DATE-TIME: "
                           + offsetdatetime);
    }
}

chevron_right


Output:

DATE-TIME: 2020-05-28T04:12:50Z

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

DATE-TIME: 2020-05-28T04:12:50Z

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced 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.