What is DurationUtils.toMillisInt() in Java?

Overview

toMillisInt() is a staticThe methods in Java that can be called without creating an object of the class. method of the DurationUtils used to convert the duration to milliseconds bound to an int datatype rather than a long datatype.

How to import DurationUtils

The definition of DurationUtils 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 DurationUtils class as follows:


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

Syntax


public static int toMillisInt(final Duration duration)

Parameters

  • final Duration duration: The duration object to convert.

Return value

This method returns milliseconds as an integer.

Code

In the code below, we convert the Duration object to milliseconds with the help of toMillisInt() method.

import org.apache.commons.lang3.time.DurationUtils;
import java.time.Duration;
public class Main{
public static void main(String[] args) {
Duration duration = Duration.ofDays(3);
System.out.printf("DurationUtils.toMillisInt(%s) = %s", duration, DurationUtils.toMillisInt(duration));
System.out.println();
}
}

Output

The output of the code will be as follows:


DurationUtils.toMillisInt(PT72H) = 259200000
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Attributions:
  1. undefined by undefined