Calendar.after()

Date

Calendar.after(date obj) Check by comparing the calendar with the date represented.

date obj : Required Calendar date object, represented.
Returns boolean , true if the date is after the Calendar date object , false otherwise.

Examples with output
Let us find out the current date , then compare this with the given ( input ) date.
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

  
Calendar my_cal = Calendar.getInstance();// created calendar 
Calendar my_present_date=Calendar.getInstance();
System.out.println("Current Date is : " + my_present_date.getTime());

//Year, Month, Date, Hour, Minute, Second
my_cal.set(2020,2, 2); // Input date
System.out.println("Input date is :" + my_cal.getTime());

System.out.println(my_cal.after(my_present_date));//true
}
}
Output is here
Current Date is : Sun Mar 01 08:49:19 IST 2020
Input date is :Mon Mar 02 08:49:19 IST 2020
true
This output will change based on the current date and input date you use in your script. As you can see we tested this program on Mar 1st by using input date as 2nd of March ( same year ), we got the output as true.

Including time

We will add Hour , minutes and Seconds to our comparison. We have kept the minutes part more than the current minute, so we get true as output.
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

  
Calendar my_cal = Calendar.getInstance();// created calendar 
Calendar my_present_date=Calendar.getInstance();
System.out.println("Current Date is: " + my_present_date.getTime());

//Year, Month, Date, Hour, Minute, Second
my_cal.set(2020,2, 1,9,19,13); // Input date
System.out.println("Input Date  is  :" + my_cal.getTime());

System.out.println(my_cal.after(my_present_date));//true
}
}
Output is here
Current Date is: Sun Mar 01 09:18:58 IST 2020
Input Date  is  :Sun Mar 01 09:19:13 IST 2020
true

With two input dates

Instead of using current date and time we can use two input dates for comparison.
As our first date ( my_cal_d1 ) is not after second date ( my_cal_d2), we will get false as output.
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

  
Calendar my_cal_d1 = Calendar.getInstance();// created calendar 
Calendar my_cal_d2 = Calendar.getInstance();

my_cal_d1.set(2020,2, 1); // Input date
my_cal_d2.set(2020,2, 2); // Input date

System.out.println(my_cal_d1.after(my_cal_d2));//false
}
}


Date & time tutorials


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer