Monday, 26 March 2018

How to use equals and compareTo method of YearMonth Class | Java 8 Date and Time


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

YearMonthDemo.java

import java.time.YearMonth;

public class YearMonthDemo
{

    public static void main(String[] args)
    {

        YearMonth yearMonth1 = YearMonth.of(2016, 03);
        System.out.println("yearMonth1 = "+yearMonth1);
       
        YearMonth yearMonth2 = YearMonth.of(2017, 03);
        System.out.println("yearMonth2 = "+yearMonth2);
       
        /*
         * Parameters:
         *
         * obj - the object to check, null returns false
         *
         * Returns:
         *
         * true if this is equal to the other year-month
         */

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

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


        int result = yearMonth1.compareTo(yearMonth2);
        System.out.println(yearMonth1 + " compare to " + yearMonth2 + " = " + result);

    }

}

Output

yearMonth1 = 2016-03
yearMonth2 = 2017-03
2016-03 is equal to 2017-03 = false
2016-03 compare to 2017-03 = -1

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/b8bbbd8e3fa258805c096cde7ca05dcc526111c2/BasicJava_2018/YearMonthDemo_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