Home »
Java »
Java Reference »
Java Duration Class
Java Duration Class | equals() Method with Example
Duration Class equals() method: Here, we are going to learn about the equals() method of Duration Class with its syntax and example.
Submitted by Preeti Jain, on May 14, 2020
Duration Class equals() method
- equals() method is available in java.time package.
- equals() method is used to identifies whether this Duration and the given object are equal or not.
- equals() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- equals() method does not throw an exception at the time of comparing two objects.
Syntax:
public boolean equals(Duration d);
Parameter(s):
- Duration d – represents the object is to be compared to this object.
Return value:
The return type of this method is boolean, it returns true when both the compared objects are equal otherwise it returns false.
Example:
// Java program to demonstrate the example
// of boolean equals(Duration d) method of Duration
import java.time.*;
import java.time.temporal.*;
public class EqualsOfDuration {
public static void main(String args[]) {
// Instantiates three Duration objects
Duration du1 = Duration.ofHours(2);
Duration du2 = Duration.ofMinutes(2);
Duration du3 = Duration.between(LocalTime.MIN, LocalTime.MAX);
System.out.println("du1: " + du1);
System.out.println("du2: " + du2);
System.out.println("du3: " + du3);
System.out.println();
// compares this Duration object(du1)
// to the given Duration object(du2) for
// equality i.e here it returns false because
// the value of du1<> du2
boolean status = du1.equals(du2);
System.out.println("du1.equals(du2): " + status);
// compares this Duration object(du2)
// to the given Duration object(du3) for
// equality i.e here it returns false because
// the value of du2 <> du3
status = du2.equals(du3);
System.out.println("du2.equals(du3): " + status);
// compares this Duration object(du3)
// to the given Duration object(du3) for
// equality i.e here it returns true because
// the value of du2 == du3
status = du3.equals(du3);
System.out.println("du3.equals(du3): " + status);
}
}
Output
du1: PT2H
du2: PT2M
du3: PT23H59M59.999999999S
du1.equals(du2): false
du2.equals(du3): false
du3.equals(du3): true