當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Calendar isSet()用法及代碼示例


Calendar類中的isSet(int calndr_field)方法用於檢查給定日曆字段是否設置了值。通過內部字段的計算為其設置值的所有Case均由get方法調用觸發。

用法:

public final boolean isSet(int calndr_field)

參數:該方法采用一個參數calndr_field,該參數表示要操作的日曆字段。


返回值:如果設置了日曆字段的值,則該方法返回True,否則返回False。

以下示例程序旨在說明Calendar類的isSet()方法的用法:

示例1:

// 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); 
    } 
}
輸出:
The Year is: 2019
Is the Value set? true
Is the Value set? false

示例2:

// 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://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#isSet-int-



相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Calendar isSet() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。