Java Duration Class | ofMillis() Method with Example

Duration Class ofMillis() method: Here, we are going to learn about the ofMillis() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 16, 2020

Duration Class ofMillis() method

  • ofMillis() method is available in java.time package.
  • ofMillis() method is used to represent the given milliseconds in this Duration.
  • ofMillis() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • ofMillis() method may throw an exception at the time of representing milliseconds.
    ArithmeticException: This exception may throw when the given milliseconds value exceeds the length of this Duration.

Syntax:

    public static Duration ofMillis(long millis_val);

Parameter(s):

  • long millis_val – represents the number of milliseconds in value.

Return value:

The return type of this method is Duration, it returns the Duration that holds the value of the given milliseconds.

Example:

// Java program to demonstrate the example 
// of ofMillis(long millis_val) method of Duration

import java.time.*;

public class OfMillisOfDuration {
    public static void main(String args[]) {
        long millis_val = 5000;

        // returns the Duration that holds the value 
        // of the given argument i.e. 
        // here we are representing
        // 5000 ms (1s = 1000 ms)in this Duration 
        // du 5000 ms in 5S
        Duration du = Duration.ofMillis(millis_val);

        // Display du
        System.out.println("Duration.ofMillis(millis_val): " + du);
    }
}

Output

Duration.ofMillis(millis_val): PT5S



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.