Open In App

LocalDate of() method in Java with Examples

Last Updated : 21 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report
  1. The of(int, int, int) method of LocalDate class in Java is used to create an instance of LocalDate from the input year, month and day of the month. In this method, all the three parameters are passed in the form of integer.

    Syntax:

    public static LocalDate of(int year,
                               int month,
                               int dayOfMonth)
    

    Parameters: This method accepts three 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.

    Return Value: This method returns the localdate.

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

    Below programs illustrate the of(int month) method in Java:
    Program 1:




    // Java program to demonstrate
    // LocalDate.of(int month) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDate object
            LocalDate localdate
                = LocalDate.of(2020, 5, 13);
      
            // print full date
            System.out.println("Date: " + localdate);
        }
    }

    
    

    Output:

    Date: 2020-05-13
    

    Program 2:




    // Java program to demonstrate
    // LocalDate.of(int month) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDate object
            LocalDate localdate
                = LocalDate.of(2020, 5, 13);
      
            // print year only
            System.out.println("Year: "
                               + localdate.getYear());
        }
    }

    
    

    Output:

    Year: 2020
    
  2. The of(int, Month, int) method of LocalDate class in Java is used to obtain an instance of LocalDate from the input year, month and day. In this method, the parameters year and day are passed as integers but the month is passes as an instance.

    Syntax:

    public static LocalDate of(int year,
                               Month month,
                               int dayOfMonth)
    

    Parameters: This method accepts three 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.

    Return Value: This method returns the localdate.

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

    Below programs illustrate the of(Month month) method in Java:
    Program 1:




    // Java program to demonstrate
    // LocalDate.of(Month month) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDate object
            LocalDate localdate = LocalDate.of(
                2020, Month.MAY, 13);
      
            // print full date
            System.out.println("Date: "
                               + localdate);
        }
    }

    
    

    Output:

    Date: 2020-05-13
    

    Program 2:




    // Java program to demonstrate
    // LocalDate.of(Month month) method
      
    import java.time.*;
    import java.time.temporal.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // create LocalDate object
            LocalDate localdate = LocalDate.of(
                2020, Month.MAY, 13);
      
            // print month only
            System.out.println("Month: "
                               + localdate.getMonth());
        }
    }

    
    

    Output:

    Month: MAY
    

References:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads