Calendar.before() method in Java
Last Updated :
25 Apr, 2018
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 :
import java.util.*;
class GFG {
public static void main(String[] args)
throws InterruptedException
{
Calendar cal_obj1 = Calendar.getInstance();
System.out.println( "Time 1 : " + cal_obj1.getTime());
Thread.sleep( 3000 );
Calendar cal_obj2 = Calendar.getInstance();
System.out.println( "Time 2 : " + cal_obj2.getTime());
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 :
import java.util.*;
class GFG {
public static void main(String[] args)
{
Calendar cal_obj1 = Calendar.getInstance();
Calendar cal_obj2 = Calendar.getInstance();
System.out.println( "Current date is : " + cal_obj1.getTime());
cal_obj2.set(Calendar.YEAR, 2010 );
System.out.println( "Year is " + cal_obj2.get(Calendar.YEAR));
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 :
import java.util.*;
class GFG {
public static void main(String[] args)
{
Calendar cal_obj1 = Calendar.getInstance();
Calendar cal_obj2 = Calendar.getInstance();
System.out.println( "Current date is : " + cal_obj1.getTime());
cal_obj2.set(Calendar.YEAR, 2025 );
System.out.println( "Year is " + cal_obj2.get(Calendar.YEAR));
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...