Skip to content
Related Articles

Related Articles

Improve Article

LocalDateTime of(Month) method in Java with Examples

  • Last Updated : 21 May, 2020
Geek Week
  1. The of(int year, Month month, int dayOfMonth, int hour, int minute) method of LocalDateTime class in Java is used to create an instance of LocalDateTime from the input year, month, day of month, hour and minute. The instance of LocalDateTime represents the date along with time. The parameters year, month and day are used to determine the date and the parameters hour and minute are used to determine the time. The second and nano-second will be set to zero by default.

    Syntax:

    public static LocalDateTime of(int year,
                                   Month month,
                                   int dayOfMonth,
                                   int hour,
                                   int minute)
    

    Parameters: The method accepts five parameters.

    • year – It is of integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
    • month – It is of Month type and represents the month of the year. It varies from JANUARY to DECEMBER.
    • dayOfMonth – 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.

    Return Value: This method returns the LocalDateTime as derived from the input value.

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

    Below programs illustrate the of(int year, Month month, int dayOfMonth, int hour, int minute) method in Java:



    Program 1:




    // Java program to demonstrate
    // LocalDateTime.of(int year, Month month,
    // int dayOfMonth, int hour, int minute) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDateTime object
            LocalDateTime localdatetime
                = LocalDateTime.of(
                    2020, Month.MAY, 13, 6, 30);
      
            // print full date and time
            System.out.println("DateTime: "
                               + localdatetime);
        }
    }
    Output:
    DateTime: 2020-05-13T06:30
    

    Program 2:




    // Java program to demonstrate
    // LocalDateTime.of(int year, Month month,
    // int dayOfMonth, int hour, int minute) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDateTime object
            LocalDateTime localdatetime
                = LocalDateTime.of(
                    2019, Month.MAY,
                    13, 6, 30);
      
            // print month only
            System.out.println(
                "Month: "
                + localdatetime.getMonth());
        }
    }
    Output:
    Month: MAY
    
  2. The of(int year, Month month, int dayOfMonth, int hour, int minute, int second) method of LocalDateTime class in Java is used to create an instance of LocalDateTime from the input year, month, day, hour, minute and second. The nanosecond is set to zero by default while creating the instance. In this method, only the month is passed as an instance while the others are integers. This method is used where nanosecond is not required.

    Syntax:

    public static LocalDateTime of(int year,
                                   Month month,
                                   int dayOfMonth,
                                   int hour,
                                   int minute,
                                   int second)
    

    Parameters: This method accepts six parameters:

    • year – It is of integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
    • month – It is of Month type and represents the month of the year. It varies from JANUARY to DECEMBER.
    • dayOfMonth – 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.

    Return Value: This method returns the localdate-time.

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



    Below programs illustrate the of(int year, Month month, int dayOfMonth, int hour, int minute, int second) method in Java:

    Program 1:




    // Java program to demonstrate
    // LocalDateTime.of(int year, Month month,
    // int dayOfMonth, int hour, int minute,
    // int second) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDateTime object
            LocalDateTime localdatetime
                = LocalDateTime.of(
                    2020, Month.MAY, 13,
                    6, 30, 45);
      
            // print full date and time
            System.out.println("DateTime: "
                               + localdatetime);
        }
    }
    Output:
    DateTime: 2020-05-13T06:30:45
    

    Program 2:




    // Java program to demonstrate
    // LocalDateTime.of(int year, Month month,
    // int dayOfMonth, int hour, int minute,
    // int second) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDateTime object
            LocalDateTime localdatetime
                = LocalDateTime.of(2019, Month.MAY,
                                   13, 6, 30, 45);
      
            // print year only
            System.out.println(
                "Year: "
                + localdatetime.getYear());
        }
    }
    Output:
    Year: 2019
    
  3. The of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoSecond) method of LocalDateTime class in Java is used to create an instance of LocalDateTime from the input year, month, day, hour, minute, second and nanosecond. The instance of LocalDateTime represents the date along with time. The parameters year, month and day are used to obtain the date and parameters hour, minute, second and nanosecond are used to obtain the time.

    Syntax:

    public static LocalDateTime of(int year,
                                   Month month,
                                   int dayOfMonth,
                                   int hour,
                                   int minute,
                                   int second,
                                   int nanoOfSecond)
    

    Parameters: This method accepts seven parameters:

    • year – It is of Integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
    • month – It is of Month type and represents the month of the year. It varies from JANUARY to DECEMBER.
    • dayOfMonth – 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.
    • nanoOfSecond – It is of Integer type and represents the nano of second. It varies from 0 to 999999999.

    Return Value: This method returns the localdate-time.

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

    Below programs illustrate the of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) method in Java:

    Program 1:




    // Java program to demonstrate
    // LocalDateTime.of(int year, Month month,
    // int dayOfMonth, int hour, int minute,
    // int second, int nanoOfSecond) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDateTime object
            LocalDateTime localdatetime
                = LocalDateTime.of(
                    2020, Month.MAY, 13, 6,
                    30, 45, 20000);
      
            // print full date and time
            System.out.println("DateTime: "
                               + localdatetime);
        }
    }
    Output:
    DateTime: 2020-05-13T06:30:45.000020
    

    Program 2:




    // Java program to demonstrate
    // LocalDateTime.of(int year, Month month,
    // int dayOfMonth, int hour, int minute,
    // int second, int nanoOfSecond) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDateTime object
            LocalDateTime localdatetime
                = LocalDateTime.of(
                    2019, Month.DECEMBER, 31, 12,
                    30, 45, 50000);
      
            // print full date and time
            System.out.println("DateTime: "
                               + localdatetime);
        }
    }
    Output:
    DateTime: 2019-12-31T12:30:45.000050
    

References:

  1. https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#of(int, java.time.Month, int, int, int)
  2. https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#of(int, java.time.Month, int, int, int, int)
  3. https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#of(int, java.time.Month, int, int, int, int, int)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :