Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaDuration equals(Duration) method in Java with Examples

Duration equals(Duration) method in Java with Examples

The equals(Duration) method of Duration Class in java.time package is used to check whether this duration is equal to the duration passed as the parameter.

Syntax:

public boolean equals(Duration otherDuration)

Parameters: This method accepts a parameter otherDuration which is the duration to which this duration will be compared to.

Return Value: This method returns a boolean value showing whether this Duration is equal to the otherDuration or not.

Below examples illustrate the Duration.equals() method:

Example 1:




// Java code to illustrate equals() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using parse() method
        Duration duration1
            = Duration.parse("P2DT3H4M");
  
        // Duration using ofDays() method
        Duration duration2
            = Duration.ofDays(10);
  
        // Compare the durations
        // using equals() method
        System.out.println(duration1
                               .equals(duration2));
    }
}


Output:

false

Example 2:




// Java code to illustrate equals() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using ofHours() method
        Duration duration1
            = Duration.ofHours(5);
  
        // Duration using ofHours() method
        Duration duration2
            = Duration.ofHours(5);
  
        // Compare the durations
        // using equals() method
        System.out.println(duration1
                               .equals(duration2));
    }
}


Output:

true

Example 3:




// Java code to illustrate equals() method
  
import java.time.Duration;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Duration using ofDays() method
        Duration duration1
            = Duration.ofDays(5);
  
        // Duration using ofHours() method
        Duration duration2
            = Duration.ofHours(5);
  
        // Compare the durations
        // using equals() method
        System.out.println(duration1
                               .equals(duration2));
    }
}


Output:

false

Reference: Oracle Doc

RELATED ARTICLES

Most Popular

Recent Comments