java.time.Duration.from() Method Example



Description

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

Declaration

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

public static Duration from(TemporalAmount amount)

Parameters

amount − the temporal amount to convert, not null.

Return Value

the equivalent duration, not null.

Exception

  • DateTimeException − if unable to convert to a Duration.

  • ArithmeticException − if numeric overflow occurs.

Example

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

package com.tutorialspoint;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

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

      Duration duration = Duration.from(ChronoUnit.DAYS.getDuration());
      System.out.println(duration.toMinutes());
   }
}

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

1440
Advertisements