Home »
Java »
Java Reference »
Java Duration Class
Java Duration Class | ofDays() Method with Example
Duration Class ofDays() method: Here, we are going to learn about the ofDays() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 16, 2020
Duration Class ofDays() method
- ofDays() method is available in java.time package.
- ofDays() method is used to represent the given number of days in this Duration.
- ofDays() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
- ofDays() method may throw an exception at the time of representing days.
ArithmeticException: This exception may throw when the given day's value exceeds the length of this Duration.
Syntax:
public static Duration ofDays(long day_val);
Parameter(s):
- long day_val – represents the number of days in value.
Return value:
The return type of this method is Duration, it returns the Duration that holds the value of the given days.
Example:
// Java program to demonstrate the example
// of ofDays(long day_val) method
// of Duration
import java.time.*;
import java.time.temporal.*;
public class OfDaysOfDuration {
public static void main(String args[]) {
long days_val = 3;
// returns the Duration that holds
// the value of the given argument i.e.
// here we are representing 3 days (72H)
// in this Duration du1
Duration du1 = Duration.ofDays(days_val);
// Display du1
System.out.println("Duration.ofDays(3): " + du1);
}
}
Output
Duration.ofDays(3): PT72H