Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
formatperiodiso

What is DurationFormatUtils.formatPeriodISO() 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.

Overview

formatPeriodISO() is a staticthe methods in Java that can be called without creating an object of the class. method of the DurationFormatUtils which is used to convert the time gap between the start time and the end time in milliseconds in 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 formatPeriodISO(final long startMillis, final long endMillis)

Parameters

  • final long startMillis: The start of the period in milliseconds.
  • final long endMillis: The end of the period in milliseconds.

Return value

This method returns true if the string is blank. Otherwise, it returns false.

Code

import org.apache.commons.lang3.time.DurationFormatUtils;
public class Main{
public static void main(String[] args) {
// Example 1
long startMillis = 1234542;
long endMillis = 12354532;
System.out.printf("DurationFormatUtils.formatPeriodISO(%s, %s) = %s", startMillis, endMillis, DurationFormatUtils.formatPeriodISO(startMillis, endMillis));
System.out.println();
// Example 2
startMillis = 0;
endMillis = 12321;
System.out.printf("DurationFormatUtils.formatPeriodISO(%s, %s) = %s", startMillis, endMillis, DurationFormatUtils.formatPeriodISO(startMillis, endMillis));
System.out.println();
}
}

Example 1

  • start milliseconds - 1234542
  • end milliseconds - 12354532

The method returns P0Y0M0DT3H5M19.990S i.e the formatted period between the start and end milliseconds to ISO 8601 period format.

Example 2

  • start milliseconds - 0
  • end milliseconds - 12321

The method returns P0Y0M0DT0H0M12.321S i.e the formatted period between the start and end milliseconds to ISO 8601 period format.

Output

The output of the code will be as follows:


DurationFormatUtils.formatPeriodISO(1234542, 12354532) = P0Y0M0DT3H5M19.990S
DurationFormatUtils.formatPeriodISO(0, 12321) = P0Y0M0DT0H0M12.321S

RELATED TAGS

java
communitycreator
formatperiodiso

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