toDaysPart()
is an instance method of the Duration
that obtains the number of days in the Duration
object. The number of days is calculated by dividing the number of seconds of the Duration
object by 86400
. This is based on the traditional 24-hour definition of a day. This method was introduced in Java version 9.
public long toDaysPart()
The method has no parameters.
This method returns the number of days in the Duration
object:
toNanosPart()
is an instance method of the Duration
which is used to obtain the number of nanoseconds part in the Duration
object. The number of nanoseconds part is the number of nanoseconds stored in the Duration
object. This method was introduced in Java version 9.
public int toNanosPart()
The method has no parameters.
This method returns the number of nanoseconds part in the Duration
object.
The toDaysPart
and toNanosPart
methods are defined in the Duration
class. The Duration
class is defined in the java.time
package. To import the Duration
class check the following import statement:
import java.time.Duration;
import java.time.Duration;public class Main {public static void daysPart(){Duration duration = Duration.ofSeconds(143234);long numOfDays = duration.toDaysPart();System.out.printf("Number of days in %s is %s", duration, numOfDays);System.out.println();}public static void nanosPart(){Duration duration = Duration.ofSeconds(143234, 4223);long numOfNanoSecondsPart = duration.toNanosPart();System.out.printf("The number of nanoseconds part in %s is %s", duration, numOfNanoSecondsPart);System.out.println();}public static void main(String[] args) {daysPart();nanosPart();}}
Duration
class.daysPart()
that creates a Duration
object using the ofSeconds()
method and prints the number of days using the toDaysPart()
method.nanosPart()
that creates a Duration
object using the ofSeconds()
method. It also prints the number of nanoseconds part using the toNanosPart()
method.