Wednesday, July 1, 2026
HomeLanguagesJavaPeriod of() method in Java with Examples

Period of() method in Java with Examples

The of() method of Period Class is used to obtain a period from given number of Years, Months, and Days as parameters. These parameters are accepted in the form of integers. This method returns a Period with the given number of years, months, and days.

Syntax:

public static Period of(
        int numberOfYears, 
        int numberOfMonths,
        int numberOfDays)

Parameters: This method accepts following parameters:

  • numberOfYears: This is used to represent the amount of years which may be negative.
  • numberOfMonths: This is used to represent the amount of months which may be negative.
  • numberOfDays: This is used to represent the amount of days which may be negative.

Returns: This function returns the period which is the Period object parsed with the given number of Years, Months, and Days.

Below is the implementation of Period.of() method:

Example 1:




// Java code to demonstrate of() method
  
import java.time.Period;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Get the number of Years
        int numberOfYears = 1;
  
        // Get the number of Months
        int numberOfMonths = 5;
  
        // Get the number of Days
        int numberOfDays = 21;
  
        // Parse the given amounts into Period
        // using of() method
        Period p
            = Period.of(numberOfYears,
                        numberOfMonths,
                        numberOfDays);
  
        System.out.println(p.getYears() + " Years\n"
                           + p.getMonths() + " Months\n"
                           + p.getDays() + " Days");
    }
}


Output:

1 Years
5 Months
21 Days

Example 2:




// Java code to demonstrate of() method
  
import java.time.Period;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Get the number of Years
        int numberOfYears = -2;
  
        // Get the number of Months
        int numberOfMonths = 10;
  
        // Get the number of Days
        int numberOfDays = -11;
  
        // Parse the given amounts into Period
        // using of() method
        Period p
            = Period.of(numberOfYears,
                        numberOfMonths,
                        numberOfDays);
  
        System.out.println(p.getYears() + " Years\n"
                           + p.getMonths() + " Months\n"
                           + p.getDays() + " Days");
    }
}


Output:

-2 Years
10 Months
-11 Days

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#of-int-int-int-

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32517 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6966 POSTS0 COMMENTS