Java Duration Class | toDays() Method with Example

Duration Class toDays() method: Here, we are going to learn about the toDays() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 17, 2020

Duration Class toDays() method

  • toDays() method is available in java.time package.
  • toDays() method is used to convert this Duration into the number of days.
  • toDays() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • toDays() method does not throw an exception at the time of converting this Duration to days.

Syntax:

    public Temporal subtractFrom(Temporal temp);

Parameter(s):

  • None

Return value:

The return type of this method is long, it returns the number of days exists in this Duration.

Example:

// Java program to demonstrate the example 
// of long toDays() method of Duration

import java.time.*;

public class ToDaysOfDuration {
    public static void main(String args[]) {
        // Instantiates two Duration objects
        Duration du1 = Duration.ofHours(72);
        Duration du2 = Duration.parse("P0DT48H");

        // Display du1 and du2
        System.out.println("du1: " + du1);
        System.out.println("du2: " + du2);

        // represents this Duration du1 in hours into
        // number of days i.e. 72H = 3 days
        long to_days = du1.toDays();

        // Display to_days
        System.out.println("du1.toDays(): " + to_days);
        
        // represents this Duration du2 in hours into
        // number of days i.e. 78H = 2 days
        to_days = du2.toDays();

        // Display to_days
        System.out.println("du2.toDays(): " + to_days);
    }
}

Output

du1: PT72H
du2: PT48H
du1.toDays(): 3
du2.toDays(): 2


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.