What is DurationFormatUtils.formatDurationISO() in Java?

What is DurationFormatUtils.formatDurationISO() in Java? Answer

Overview

formatDurationISO() is a static method of the DurationFormatUtils class that is used to convert the time gap in milliseconds to ISO 8601 period format.

How to import DurationFormatUtils

The definition of DurationFormatUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file.


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the DurationFormatUtils class as follows.


import org.apache.commons.lang3.time.DurationFormatUtils;

Syntax


public static String formatDurationISO(final long durationMillis)

Parameters

  • final long durationMillis: The time gap in milliseconds to format.

Return value

This method returns the formatted duration.

Code

The code below shows how the formatDurationISO() works in Java.

import org.apache.commons.lang3.time.DurationFormatUtils;

public class Main{

    public static void main(String[] args) {
        // Example 1
        long durationMillis = 1234542;
        System.out.printf("DurationFormatUtils.formatDurationISO(%s) = %s", durationMillis, DurationFormatUtils.formatDurationISO(durationMillis));
        System.out.println();

        // Example 2
        durationMillis = 0;
        System.out.printf("DurationFormatUtils.formatDurationISO(%s) = %s", durationMillis, DurationFormatUtils.formatDurationISO(durationMillis));
        System.out.println();

    }
}

Output

The output of the code will be as follows:


DurationFormatUtils.formatDurationISO(1234542) = P0Y0M0DT0H20M34.542S
DurationFormatUtils.formatDurationISO(0) = P0Y0M0DT0H0M0.000S

Explanation

Example 1

  • duration in milliseconds - 1234542

The method returns P0Y0M0DT0H20M34.542S, i.e., the formatted duration in milliseconds to ISO 8601 period format.

Example 2

  • start milliseconds - 0
  • end milliseconds - 12321

The method returns P0Y0M0DT0H0M12.321S, i.e., the formatted duration in milliseconds to ISO 8601 period format.

What is DurationFormatUtils.formatDurationISO() in Java? Review:

In our experience, we suggest you solve this What is DurationFormatUtils.formatDurationISO() in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is DurationFormatUtils.formatDurationISO() in Java?

Find on Educative

Conclusion:

I hope this What is DurationFormatUtils.formatDurationISO() in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.