Monday 21 May 2018

How to use compareTo and equals methods of Instant class | Java 8 Date and Time


Click here to watch on Youtube:
https://www.youtube.com/watch?v=ZwMiXZkLW3Y&list=UUhwKlOVR041tngjerWxVccw

InstantDemo.java

import java.time.Instant;

public class InstantDemo
{
    public static void main(String[] args)
    {

        Instant instant1 = Instant.parse("2014-12-03T10:15:30.00Z");
        System.out.println("instant1 = "+instant1);
       
        Instant instant2 = Instant.parse("2017-12-03T10:15:30.00Z");
        System.out.println("instant2 = "+instant2);
       
        /*
         * Parameters:
         *
         * otherInstant - the other instant to compare to, not null
         *
         * Returns:
         *
         * the comparator value, negative if less, positive if greater
         */

        int value = instant1.compareTo(instant2);
        System.out.println(value);
       
        /*
         * Parameters:
         *
         * otherInstant - the other instant, null returns false
         *
         * Returns:
         *
         * true if the other instant is equal to this one
         */

        boolean isEqual = instant1.equals(instant2);
        System.out.println(isEqual);
       
       
    }

}

Output

instant1 = 2014-12-03T10:15:30Z
instant2 = 2017-12-03T10:15:30Z
-1
false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/InstantDemo_compareTo_equals.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/InstantDemo_compareTo_equals

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/80c7c27095bd0aa4eb0109eac12d92549dcd717d/BasicJava_2018/InstantDemo_compareTo_equals/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment