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

LocalDateTime of(int) method in Java with Examples

  1. The of(int year, int 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, 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,
                                   int month,
                                   int dayOfMonth,
                                   int hour,
                                   int minute)
    

    Parameters: This 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 integer type and represents the month of the year. It varies from 1(JANUARY) to 12(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 localdate-time 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, int month, int dayOfMonth, int hour, int minute) method in Java:

    Program 1:




    // Java program to demonstrate
    // LocalDateTime.of(int year, int 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, 5, 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, int 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, 5, 13, 6, 30);
      
            // print year only
            System.out.println(
                "Year: "
                + localdatetime.getYear());
        }
    }

    
    
    Output:

    Year: 2019
    
  2. The of(int year, int 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 parameters year, month and day are used to determine the date and the parameters hour, minute and second are used to determine the time. This method is used where nanosecond is not required as the value of nanoSecond will be set to zero by default.

    Syntax:

    public static LocalDateTime of(int year,
                                   int 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 integer type and represents the month of the year. It varies from 1(JANUARY) to 12(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 as computed 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, int month, int dayOfMonth, int hour, int minute, int second) method in Java:

    Program 1:




    // Java program to demonstrate
    // LocalDateTime.of(int year, int 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, 5, 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, int 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, 5, 13, 6, 30, 45);
      
            // print dayofmonth only
            System.out.println(
                "DayOfMonth: "
                + localdatetime.getDayOfMonth());
        }
    }

    
    
    Output:

    DayOfMonth: 13
    
  3. The of(int year, int 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 for date and parameters hour, minute, second and nanosecond are used for time.

    Syntax:

    public static LocalDateTime of(int year,
                                   int 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 integer type and represents the month of the year. It varies from 1(JANUARY) to 12(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 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, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) method in Java:

    Program 1:




    // Java program to demonstrate
    // LocalDateTime.of(int year, int 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, 5, 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, int 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, 5, 13, 6,
                    30, 45, 50000);
      
            // print nanoOfSecond only
            System.out.println(
                "NanoOfSecond: "
                + localdatetime.getNano());
        }
    }

    
    
    Output:

    NanoOfSecond: 50000
    

References:

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

Most Popular

Recent Comments