Friday, May 15, 2026
HomeLanguagesJavaChronoUnit getDuration() method in Java with Examples

ChronoUnit getDuration() method in Java with Examples

The getDuration() method of ChronoUnit enum is used to return the estimated duration of this ChronoUnit in the ISO calendar system.Days vary due to daylight saving time, while months have different lengths.

Syntax:

public Duration getDuration()

Parameters: This method accepts nothing.

Return value: This method returns the estimated duration of this unit, not null.

Below programs illustrate the ChronoUnit.getDuration() method:
Program 1:




// Java program to demonstrate
// ChronoUnit.getDuration() method
  
import java.time.Duration;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get ChronoUnit
        ChronoUnit chronoUnit
            = ChronoUnit.valueOf("NANOS");
  
        // apply getDuration()
        Duration getDurationAttribute
            = chronoUnit.getDuration();
  
        // print
        System.out.println(
            "Duration Estimated :"
            + getDurationAttribute);
    }
}


Output:

Duration Estimated :PT0.000000001S

Program 2:




// Java program to demonstrate
// ChronoUnit.getDuration() method
  
import java.time.Duration;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get ChronoUnit
        ChronoUnit chronoUnit
            = ChronoUnit.valueOf("DAYS");
  
        // apply getDuration()
        Duration getDurationAttribute
            = chronoUnit.getDuration();
  
        // print
        System.out.println(
            "Duration Estimated :"
            + getDurationAttribute);
    }
}


Output:

Duration Estimated :PT24H

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/ChronoUnit.html#getDuration()

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS