java.util.Calendar.before() is a method in Calendar class of java.util package. The method returns true if the time represented by this Calendar is before the time represented by when Object. If it is not the case, false is returned.
Syntax :
public boolean before(Object when) Where, when is the Object that is to be compared.
Below are some examples to understand the implementation of the Calendar.before() function in a better way.
Example 1 :
// Implementation to show the usage // of before(Object when) method of // Calendar class import java.util.*; class GFG { public static void main(String[] args) throws InterruptedException { // creating calendar object Calendar cal_obj1 = Calendar.getInstance(); // printing current date System.out.println("Time 1 : " + cal_obj1.getTime()); // sleep for 3 seconds Thread.sleep(3000); // creating Calendar object Calendar cal_obj2 = Calendar.getInstance(); // printing current date System.out.println("Time 2 : " + cal_obj2.getTime()); // checking if 1st date is before 2nd date // and printing the result System.out.println(cal_obj1.before(cal_obj2)); } } |
Output :
Time 1 : Wed Feb 28 15:43:19 IST 2018 Time 2 : Wed Feb 28 15:43:22 IST 2018 true
Example 2 :
// Implementation to show the usage // of before(Object when) method of // Calendar class import java.util.*; class GFG { public static void main(String[] args) { // creating calendar objects Calendar cal_obj1 = Calendar.getInstance(); Calendar cal_obj2 = Calendar.getInstance(); // displaying the current date System.out.println("Current date is : " + cal_obj1.getTime()); // changing year in cal_obj2 calendar cal_obj2.set(Calendar.YEAR, 2010); // displaying the year System.out.println("Year is " + cal_obj2.get(Calendar.YEAR)); // check if calendar date is before current date System.out.println("Result : " + cal_obj1.before(cal_obj2)); } } |
Output :
Current date is : Wed Feb 28 15:50:16 IST 2018 Year is 2010 Result : false
Example 3 :
// Implementation to show the usage // of before(Object when) method of // Calendar class import java.util.*; class GFG { public static void main(String[] args) { // creating calendar objects Calendar cal_obj1 = Calendar.getInstance(); Calendar cal_obj2 = Calendar.getInstance(); // displaying the current date System.out.println("Current date is : " + cal_obj1.getTime()); // changing year in cal_obj2 calendar cal_obj2.set(Calendar.YEAR, 2025); // displaying the year System.out.println("Year is " + cal_obj2.get(Calendar.YEAR)); // check if calendar date is before current date System.out.println("Result : " + cal_obj1.before(cal_obj2)); } } |
Output :
Current date is : Wed Feb 28 16:15:55 IST 2018 Year is 2025 Result : true
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.Short toString() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.util.ArrayList.addall() method in Java
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java.math.BigInteger.probablePrime() method in Java
- Java.math.BigInteger.modInverse() method in Java
- Java.lang.string.replace() method in Java
- Java Clock withZone() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java.util.ArrayList.add() Method in Java
- Java.util.BitSet.set() method in Java
- Method Class | getAnnotatedReturnType() method in Java
- Method Class | getParameterTypes() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

