📜  Java中的日历 isSet() 方法及示例

📅  最后修改于: 2022-05-13 01:55:20.147000             🧑  作者: Mango

Java中的日历 isSet() 方法及示例

Calendar 类中的isSet(int calndr_field )方法用于检查给定的日历字段是否设置了值。所有已通过内部字段计算设置值的案例均由 get 方法调用触发。

句法:

public final boolean isSet(int calndr_field)

参数:该方法采用一个参数calndr_field ,该参数引用要操作的日历字段。
返回值:如果设置了日历字段的值,则该方法返回True ,否则返回False

下面的程序说明了 Calendar 类的 isSet() 方法的工作原理:

示例 1:

Java
// Java code to illustrate
// isSet() method
   
import java.util.*;
public class CalendarDemo {
    public static void main(String args[])
    {
   
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
   
        // Displaying the calendar object
        System.out.println("The Year is: "
                           + calndr.get(Calendar.YEAR));
   
        // Querying for the value if set or not
        boolean val = calndr.isSet(Calendar.YEAR);
        System.out.println("Is the"
                           + " Value set? " + val);
   
        // Clearing the instance
        calndr.clear(Calendar.YEAR);
   
        // Performing isSet() operation
        val = calndr.isSet(Calendar.YEAR);
        System.out.println("Is the"
                           + " Value set? " + val);
    }
}


Java
// Java code to illustrate
// isSet() method
   
import java.util.*;
public class CalendarDemo {
    public static void main(String args[])
    {
   
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
   
        // Displaying the calendar object
        System.out.println("Todays Day is: "
                           + calndr.get(
                                 Calendar.DAY_OF_MONTH));
   
        // Querying for the value if set or not
        boolean val
            = calndr.isSet(Calendar.DAY_OF_MONTH);
        System.out.println("Is the"
                           + " Value set? " + val);
   
        // Clearing the instance
        calndr.clear(Calendar.DAY_OF_MONTH);
   
        // Performing isSet() operation
        val = calndr.isSet(Calendar.DAY_OF_MONTH);
        System.out.println("Is the"
                           + " Value set? " + val);
    }
}


输出:
The Year is: 2019
Is the Value set? true
Is the Value set? false

示例 2:

Java

// Java code to illustrate
// isSet() method
   
import java.util.*;
public class CalendarDemo {
    public static void main(String args[])
    {
   
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
   
        // Displaying the calendar object
        System.out.println("Todays Day is: "
                           + calndr.get(
                                 Calendar.DAY_OF_MONTH));
   
        // Querying for the value if set or not
        boolean val
            = calndr.isSet(Calendar.DAY_OF_MONTH);
        System.out.println("Is the"
                           + " Value set? " + val);
   
        // Clearing the instance
        calndr.clear(Calendar.DAY_OF_MONTH);
   
        // Performing isSet() operation
        val = calndr.isSet(Calendar.DAY_OF_MONTH);
        System.out.println("Is the"
                           + " Value set? " + val);
    }
}
输出:
Todays Day is: 21
Is the Value set? true
Is the Value set? false

参考: https: Java/util/Calendar.html#isSet-int-