Monday, 2 July 2018

How to use equals(Object obj) and compareTo(Date anotherDate) methods of Java.util.Date


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

DateDemo.java

import java.util.Calendar;
import java.util.Date;

public class DateDemo
{
    public static void main(String[] args)
    {
        Date date1 = Calendar.getInstance().getTime();
        System.out.println("date1 = " + date1);
       
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, 2000);
        Date date2 = cal.getTime();
        System.out.println("date2 = " + date2);

        /*
         * Parameters:
         *
         * anotherDate - the Date to be compared.
         *
         * Returns:
         *
         * the value 0 if the argument Date is equal to this Date; a
         * value less than 0 if this Date is before the Date argument;
         * and a value greater than 0 if this Date is after the Date
         * argument.
         */

        int result = date1.compareTo(date2);       
        System.out.println(result);
       
        /*
         * Parameters:
         *
         * obj - the object to compare with.
         *
         * Returns:
         *
         * true if the objects are the same; false otherwise.
         */

        boolean isEqual = date1.equals(date2);
        System.out.println(isEqual);
       
    }

}

Output

date1 = Sun May 13 11:09:42 IST 2018
date2 = Sat May 13 11:09:47 IST 2000
1
false

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

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

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