Calendar.before(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 before 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, 3); // Input date
System.out.println("Input date is :" + my_cal.getTime());
System.out.println(my_cal.before(my_present_date));//true
}
}
Output is here
Current Date is : Mon Mar 02 12:38:43 IST 2020
Input date is :Tue Mar 03 12:38:43 IST 2020
false
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 2nd by using input date as 3rd of March ( same year ), we got the output as false.
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 false 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, 2,12,51,13); // Input date
System.out.println("Input Date is :" + my_cal.getTime());
System.out.println(my_cal.before(my_present_date));//true
}
}
Output is here
Current Date is: Mon Mar 02 12:42:06 IST 2020
Input Date is :Mon Mar 02 12:51:13 IST 2020
false
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 before second date ( my_cal_d2), we will get true 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.before(my_cal_d2));// true
}
}