Tuesday, 24 July 2018

How to use before(Object when) and after(Object when) methods of Java.util.calendar class


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

CalendarDemo.java

import java.util.Calendar;

public class CalendarDemo
{
    public static void main(String[] args)
    {
        Calendar cal1 = Calendar.getInstance();
        System.out.println("Current date = " + cal1.getTime());
       
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.YEAR, 2030);
        System.out.println("future date  = " + cal2.getTime());

        /*
         * Parameters:
         *
         * when - the Object to be compared
         *
         * Returns:
         *
         * true if the time of this Calendar is before the time
         * represented by when; false otherwise.
         */

        boolean isBefore = cal1.before(cal2);
        System.out.println("is cal1 before cal2 = " + isBefore);

        /*
         * Parameters:
         *
         * when - the Object to be compared
         *
         * Returns:
         *
         * true if the time of this Calendar is after the time
         * represented by when; false otherwise.
         */

        boolean isAfter = cal1.after(cal2);
        System.out.println("is cal1 After  cal2 = " + isAfter);

    }
}

Output

Current date = Tue Jul 03 09:39:19 IST 2018
future date  = Wed Jul 03 09:39:20 IST 2030
is cal1 before cal2 = true
is cal1 After  cal2 = false

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

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

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