java.util.Calendar.before()是java.util包Calendar类中的方法。如果此Calendar表示的时间早于when对象表示的时间,则该方法返回true。如果不是这种情况,则返回false。
用法:
public boolean before(Object when) Where, when is the Object that is to be compared.
下面是一些示例,以更好的方式理解Calendar.before()函数的实现。
示例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));
}
}
输出:
Time 1 : Wed Feb 28 15:43:19 IST 2018 Time 2 : Wed Feb 28 15:43:22 IST 2018 true
示例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));
}
}
输出:
Current date is : Wed Feb 28 15:50:16 IST 2018 Year is 2010 Result : false
示例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));
}
}
输出:
Current date is : Wed Feb 28 16:15:55 IST 2018 Year is 2025 Result : true
相关用法
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java.math.BigInteger.modInverse()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Java.math.BigInteger.probablePrime()用法及代码示例
- Java Java.util.ArrayList.addall()用法及代码示例
- Java Java.lang.string.replace()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Calendar.before() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。