java.time.Duration.toString() Method Example



Description

The java.time.Duration.toString() method gets a string representation of this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.

Declaration

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

public String toString()

Return Value

an ISO-8601 representation of this duration, not null.

Exception

ArithmeticException − if numeric overflow occurs.

Example

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

package com.tutorialspoint;

import java.time.Duration;

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

      Duration duration = Duration.ofDays(2);
      System.out.println(duration.toString());  
   }
}

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

PT48H
Advertisements