java.time.Period.from() Method Example



Description

The java.time.Period.from() method obtains an instance of Period from a temporal amount.

Declaration

Following is the declaration for java.time.Period.from() method.

public static Period from(TemporalAmount amount)

Parameters

amount − the temporal amount to convert, not null.

Return Value

the equivalent Period, not null.

Exception

  • DateTimeException − if unable to convert to a Period.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Period.from() method.

package com.tutorialspoint;

import java.time.Period;

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

      Period period = Period.from(Period.of(20, 10, 5));
      System.out.println(period);
   }
}

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

P20Y10M5D
Advertisements