Java LocalDate Class | of() Method with Example

LocalDate Class of() method: Here, we are going to learn about the of() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on June 03, 2020

LocalDate Class of() method

Syntax:

public static LocalDate of(int yyyy, int mm, int day_of_mon);
public static LocalDate of(int yyyy, Month mm, int day_of_mon);
  • of() method is available in java.time package.
  • of(int yyyy, int mm, int day_of_mon) method is used to create an instance of LocalDate object from the given parameter year, month and day-of-month.
  • of(int yyyy, Month mm, int day_of_mon) method is used to create an instance of LocalDate object from the given parameter year, Month and day-of-month.
  • These methods don’t throw an exception at the time of represent date.
  • These are static methods and it is accessible with class name and if we try to access these methods with class object then we will not get an error.

Parameter(s):

  • In the first cases, of(int yyyy, int mm, int day_of_mon),
    • int yyyy – represents the year to denote this object and the range of year starts from MIN_YEAR to MAX_YEAR.
    • int mm – represents the month of year to denote this object and the number of months in a year starts from 1 and ends at 12.
    • int day_of_mon – represents the day-of-month to denote this object and the number of days in a month starts from 1 and ends at 31.
  • In the second cases, of(int yyyy, Month mm, int day_of_mon),
    • int yyyy – Similar as defined in the first case.
    • Month mm – represents the month of year defined in enum Month.
    • int day_of_mon – Similar as defined in the first case.

Return value:

In both the cases, the return type of the method is LocalDate, it returns an instance of LocalDate created from the given parameters.

Example:

// Java program to demonstrate the example 
// of of() method of LocalDate

import java.time.*;

public class OfLocalDate {
    public static void main(String args[]) {
        int year = 2005;
        int day_of_month = 10;
        int month = 5;

        // Here, of(int,int,int) creates a 
        // LocalDate from a year, month and
        // day-of-month
        LocalDate l_da = LocalDate.of(year, month, day_of_month);

        // Display l_da
        System.out.println("LocalDate.of(year,month,day_of_month): " + l_da);

        // Here, of(int,Month,int) creates a
        // LocalDate from a year, Month and 
        // day-of-month
        l_da = LocalDate.of(year, Month.JUNE, day_of_month);

        // Display l_da
        System.out.println("LocalDate.of(year,Month.JUNE,day_of_month): " + l_da);
    }
}

Output

LocalDate.of(year,month,day_of_month): 2005-05-10
LocalDate.of(year,Month.JUNE,day_of_month): 2005-06-10



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.