Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Duration.ofDays() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Introduction

ofDays is a staticThe methods in Java that can be called without creating an object of the class. method of the Duration class that is used get a Duration class instance that corresponds to the number of regular 24-hour days.

The seconds are determined with the usual definition of a day, which is 86400 seconds or 24 hours every day.

Import the Duration class

ofDays is defined in the Duration class, which is defined in the java.time package.

To import the Duration class, use the import statement below:


import java.time.Duration;

Syntax


public static Duration ofDays(long days)

Parameters

  • long days: The number of days. This parameter can be positive or negative.

Return value

The method returns a Duration object.

Code

In the code below, we create two Duration class objects with a positive and negative number of days. Then, we print the objects to the console.

import java.time.Duration;
public class Main{
public static void main(String[] args) {
// calling a function for positive days
Duration positiveDays = Duration.ofDays(5);
// printing
System.out.println("Positive Days - " + positiveDays);
// calling a function for negative days
Duration negativeDays = Duration.ofDays(-5);
// printing
System.out.println("Negative Days - " + negativeDays);
}
}

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring