Monday 2 July 2018

How to use after(Date when) and before(Date when) methods of Java.util.Date | Java Date and Time


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=0W_eoAEm2es&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:
         *
         * when - a date.
         *
         * Returns:
         *
         * true if and only if the instant represented by this Date
         * object is strictly later than the instant represented by
         * when; false otherwise.
         */

        System.out.println("Is date1 after date2 ? : " + date1.after(date2));
       
        /*
         * Parameters:
         *
         * when - a date.
         *
         * Returns:
         *
         * true if and only if the instant of time represented by this
         * Date object is strictly earlier than the instant
         * represented by when; false otherwise.
         */

        System.out.println("Is date1 before date2 ? : " + date1.before(date2));
    }

}

Output

date1 = Sat May 12 09:22:51 IST 2018
date2 = Fri May 12 09:22:52 IST 2000
Is date1 after date2 ? : true
Is date1 before date2 ? : false

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

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

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