June 29, 2022

java.time.Duration Class With Examples

The java.time.Duration class is part of new date and time API added in Java 8 that represents a time-based amount of time, such as '34.5 seconds'.

Duration class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours. In addition, the DAYS unit can be used and is treated as exactly equal to 24 hours, thus ignoring daylight savings effects.

Since Duration class models time in terms of seconds and nanoseconds it has two fields one of them a long representing seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999.

of() method in Duration class

You can get an instance of Duration class using methods-

  • of(long amount, TemporalUnit unit)
  • ofDays(long days)
  • ofHours(long hours)
  • ofMillis(long millis)
  • ofMinutes(long minutes)
  • ofNanos(long nanos)
  • ofSeconds(long seconds)
  • ofSeconds(long seconds, long nanoAdjustment)
public class DurationExample {
  public static void main(String[] args) {
    // Using days- stored as hours
    Duration duration = Duration.ofDays(122);
    System.out.println("Duration in days- " + duration);
    // Using hours- stored as hours
    duration = Duration.ofHours(150);	
    System.out.println("Duration in hours- " + duration);
    // Using minutes- stored as minutes
    duration = Duration.ofMinutes(220);	
    System.out.println("Duration in Minutes- " + duration);
    // Using Seconds
    duration = Duration.ofSeconds(12);
    System.out.println("Duration in Seconds- " + duration);
    // Using milliseconds- stored as Seconds
    duration = Duration.ofMillis(11);	
    System.out.println("Duration in Millis- " + duration);
    // Using nanoseconds- stored as Seconds
    duration = Duration.ofNanos(240000);
    System.out.println("Duration in Nanos- " + duration);
    // Using Seconds and nanoseconds
    duration = Duration.ofSeconds(1200, 245);
    System.out.println("Duration in Seconds and NanoSeconds- " + duration);
Duration in days- PT2928H
Duration in hours- PT150H
Duration in Minutes- PT3H40M
Duration in Seconds- PT12S
Duration in Millis- PT0.011S
Duration in Nanos- PT0.00024S
Duration in Seconds and NanoSeconds- PT20M0.000000245S

Finding difference between two LocalTimes using between() method

There is a between() method in Java Duration class-

  • between(Temporal startInclusive, Temporal endExclusive)

using which you can obtain a Duration consisting of the number of hours, minutes, and seconds between two times.

public class DurationExample {
  public static void main(String[] args) {
    LocalTime t1 = LocalTime.of(5, 30, 56);
    LocalTime t2 = LocalTime.of(15, 35, 45);
    System.out.println("Start Time- " + t1);
    System.out.println("End Time- " + t2);
    Duration duration = Duration.between(t1, t2);
    // Total duration in hours
    System.out.println("Hours between- " + duration.toHours());
    // Total duration in Minutes
    System.out.println("Total difference in Minutes- " + duration.toMinutes());
    // Total duration in Seconds
    System.out.println("Total difference in Seconds- " + duration.toSeconds());
    // Difference between two times 
    System.out.println("Hours between " + duration.toHours());
    System.out.println("Minutes between " + duration.toMinutesPart());
    System.out.println("Seconds between " + duration.toSecondsPart());
Start Time- 05:30:56
End Time- 15:35:45
Hours between 10
Total difference in Minutes- 604
Total difference in Seconds- 36289
Hours between 10
Minutes between 4
Seconds between 49

Here notice the difference between toMinutes() and toMinutesPart() methods and between toSeconds() and toSecondsPart() methods.

  1. toMinutes()- Gets the number of minutes in this duration.
  2. toMinutesPart()- Extracts the number of minutes part in the duration.
  1. toSeconds()- Gets the number of seconds in this duration.
  2. toSecondsPart()- Extracts the number of seconds part in the duration.

Plus or minus days, hours, minutes, seconds from the given time

Using plus(long amountToAdd, TemporalUnit unit) and minus(long amountToSubtract, TemporalUnit unit) methods you can add or subtract a Duration from the given LocalTime. Note that Duration class implements TemporalAmount.

public class DurationExample {
  public static void main(String[] args) {
    LocalTime lt = LocalTime.of(5, 30, 56);
    System.out.println("LocalTime- " + lt);
    System.out.println("Plus 2 hours " + lt.plus(Duration.ofHours(2))); 
    System.out.println("Plus 5 Minutes " + lt.plus(Duration.ofMinutes(5))); 
    System.out.println("Plus 20 Seconds " + lt.plus(Duration.ofSeconds(20))); 
LocalTime- 05:30:56
Plus 2 hours 07:30:56
Plus 5 Minutes 05:35:56
Plus 20 Seconds 05:31:16
public class DurationExample {
  public static void main(String[] args) {
    LocalTime lt = LocalTime.of(5, 30, 56);
    System.out.println("LocalTime- " + lt);
    System.out.println("Minus 2 hours " + lt.minus(Duration.ofHours(2))); 
    System.out.println("Minus 45 Minutes " + lt.minus(Duration.ofMinutes(45))); 
    System.out.println("Minus 20 Seconds " + lt.minus(Duration.ofSeconds(20))); 
LocalTime- 05:30:56
Minus 2 hours 03:30:56
Minus 45 Minutes 04:45:56
Minus 20 Seconds 05:30:36

Convert String to Duration – parse() method

  • parse(CharSequence text)- Obtains a Duration from a text string such as PnDTnHnMn.nS.

In the format "PnDTnHnMn.nS" nD represents number of days, T represents Time, nH represents number of hours, nM represents number of minutes, nS represents number of seconds. Letters in the format are accepted in upper or lower case.

public class DurationExample {
  public static void main(String[] args) {
    Duration duration = Duration.parse("PT20.345S");
    System.out.println("Duration- " + duration); 

    duration = Duration.parse("P2DT3H4M");
    System.out.println("Duration- " + duration); 
    duration = Duration.parse("PT-6H3M");
    System.out.println("Duration- " + duration); 
Duration- PT20.345S
Duration- PT51H4M
Duration- PT-5H-57M
That's all for the topic java.time.Duration Class With Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like

No comments:

Post a Comment