LocalDateTime of(date, time) method in Java with Examples

The of(LocalDate date, LocalTime time) method of LocalDateTime class in Java is used to obtain an instance of LocalDateTime using the two input parameters, date and time. Initially, two separate instances are created. One is of LocalDate type and the other one is of LocalTime type. Then these are merged to create a LocalDateTime.

Syntax:

public static LocalDateTime of(LocalDate date, 
                               LocalTime time)

Parameters: This method accepts two parameters:

  • date– It is of LocalDate type and represents the local date.
  • time– It is of LocalTime type and represents the local time.

Return Value: This method returns the localdate-time.

Exceptions: This method does not throw any exception.



Below programs illustrate the of(LocalDate date, LocalTime time) method in Java:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDateTime.of(LocalDate date,
// LocalTime time) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create LocalDate object
        // using LocalDate.of() method
        LocalDate date
            = LocalDate.of(2020, 5, 13);
  
        // Create LocalTime object
        // using LocalTime.of() method
        LocalTime time = LocalTime.of(6, 30);
  
        // Create LocalDateTime object
        LocalDateTime localdatetime
            = LocalDateTime.of(date, time);
  
        // Print full date and time
        System.out.println(
            "DateTime: " + localdatetime);
    }
}

chevron_right


Output:

DateTime: 2020-05-13T06:30

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDateTime.of(LocalDate date,
// LocalTime time) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create LocalDate object
        // using LocalDate.of() method
        LocalDate date
            = LocalDate.of(2019, 12, 31);
  
        // Create LocalTime object
        // using LocalTime.of() method
        LocalTime time = LocalTime.of(6, 30, 45);
  
        // Create LocalDateTime object
        LocalDateTime localdatetime
            = LocalDateTime.of(date, time);
  
        // Print full date and time
        System.out.println(
            "DateTime: " + localdatetime);
    }
}

chevron_right


Output:

DateTime: 2019-12-31T06:30:45

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

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.