Friday 16 March 2018

How to use equals and compareTo methods of Year Class | Java 8 Date and Time


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

YearDemo.java

import java.time.Year;

public class YearDemo
{

    public static void main(String[] args)
    {
        Year year1 = Year.of(2014);
        System.out.println("year1 = "+year1);
       
        Year year2 = Year.of(2018);
        System.out.println("year2 = "+year2);

        /*
         * Parameters:
         *
         * obj - the object to check, null returns false
         *
         * Returns:
         *
         * true if this is equal to the other year
         */

        boolean isEqual = year1.equals(year2);
        System.out.println(year1+" is equal to "+year2+" = "+isEqual);

        /*
         * Parameters:
         *
         * other - the other year to compare to, not null
         *
         * Returns:
         *
         * the comparator value, negative if less, positive if greater
         */


        int result = year1.compareTo(year2);
        System.out.println(year1+" compare to "+year2+" = "+result);
    }

}

Output

year1 = 2014
year2 = 2018
2014 is equal to 2018 = false
2014 compare to 2018 = -4

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/8e3f14caa10b52912934997a22df069c728f47b4/BasicJava_2018/YearDemo_equals_compareto/?at=master

See also:

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

    Post a Comment