java.time.Period.of() Method Example



Description

The java.time.Period.of(int years, int months, int days) method obtains a Period representing an amount in the specified unit.

Declaration

Following is the declaration for java.time.Period.of(int years, int months, int days) method.

public static Period of(int years, int months, int days)

Parameters

  • years − the amount of years, may be negative.

  • months − the amount of months, may be negative.

  • days − the amount of days, may be negative.

Return Value

a Period, not null.

Example

The following example shows the usage of java.time.Period.of(int years, int months, int days) method.

package com.tutorialspoint;

import java.time.Period;

public class PeriodDemo {
   public static void main(String[] args) {

      Period period = Period.of(1,5,2);
      System.out.println("Years: " + period.getYears() 
         + ", Months: " + period.getMonths()
         +", Days: " + period.getDays());     
   }
}

Let us compile and run the above program, this will produce the following result −

Years: 1, Months: 5, Days: 2
Advertisements