Java Instant Class | equals() Method with Example

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

Instant Class equals() method

  • equals() method is available in java.time package.
  • equals() method is used to identifies whether this Instant 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(Object o);

Parameter(s):

  • Object o – 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(Object o) method of Instant

import java.time.*;

public class EqualsOfInstant {
    public static void main(String args[]) {
        // Instantiates two Instant
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.parse("2007-06-05T10:20:30.00Z");

        // Display ins1,ins2
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);

        System.out.println();

        // Here, this method compares this Instant
        // (ins1) to the given Instant(ins2) 
        // for equality i.e. here it returns 
        // false because ins1 <> ins2
        boolean status = ins1.equals(ins2);

        // Display status
        System.out.println("ins1.equals(ins2): " + status);

        // Here, this method compares this Instant
        // (ins1) to the given Instant(ins1) 
        // for equality i.e. here it returns 
        // true because ins1 == ins2
        status = ins1.equals(ins1);

        // Display status
        System.out.println("ins1.equals(ins1): " + status);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2007-06-05T10:20:30Z

ins1.equals(ins2): false
ins1.equals(ins1): true


Comments and Discussions!

Load comments ↻





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