Java Calendar isSet()方法
java.util.Calendar类的isSet()方法是返回布尔值的最终方法。此方法检查是否设置了传递的参数值。如果未设置,则返回false,否则返回true
语法
public final boolean isSet(int field)
参数
field-日历字段。
返回
返回布尔值。
抛出
不抛出异常。
示例1
import java.util.*;
public class JavaCalendarisSetExample1 {
public static void main(String[] args) {
// create a calendar object calobj
Calendar calobj = Calendar.getInstance();
// display the current date
System.out.println(" Today " + calobj.getTime());
// determine if the given calendar field has a value set
boolean b = calobj.isSet(Calendar.YEAR);
System.out.println("Year is set: " + b);
calobj.clear(Calendar.YEAR);
// determine if the given calendar field has a value set
b = calobj.isSet(Calendar.YEAR);
System.out.println("Year is set: " + b);
}
}
输出:
Today Mon Aug 13 22:21:16 PDT 2018
Year is set: true
Year is set: false
示例2
import java.util.*;
public class JavaCalendarisSetExample2 {
public static void main(String[] args) {
// create a calendar object calobj
Calendar calobj = Calendar.getInstance();
// display the current date
System.out.println(" Today " + calobj.getTime());
// determine if the given calendar field has a value set
boolean b = calobj.isSet(Calendar.YEAR);
System.out.println("Year is set: " + b);
boolean M = calobj.isSet(Calendar.MONTH);
System.out.println("Month is set: " + M);
boolean d = calobj.isSet(Calendar.DAY_OF_WEEK);
System.out.println("Day of week is set: " + d);
calobj.clear();
// determine if the given calendar field has a value set
System.out.println("After clearing calendar");
b = calobj.isSet(Calendar.YEAR);
System.out.println("Year is set: " + b);
M = calobj.isSet(Calendar.MONTH);
System.out.println("Month is set: " + M);
d = calobj.isSet(Calendar.DAY_OF_WEEK);
System.out.println("Day of week is set: " + d);
}
}
输出:
Today Mon Aug 13 22:29:19 PDT 2018
Year is set: true
Month is set: true
Day of week is set: true
After clearing calendar
Year is set: false
Month is set: false
Day of week is set: false