Java Calendar isSet() 方法



描述

Java Calendar isSet(int field) 方法檢查給定的日曆field是否已設定值。這包括由 get 方法呼叫觸發的內部欄位計算已設定值的情況。

宣告

以下是java.util.Calendar.isSet方法的宣告

public final boolean isSet(int field)

引數

field − 要檢查的欄位。

返回值

如果給定的日曆欄位已設定值,則此方法返回true;否則返回false

異常

檢查當前日期日曆例項中月份天數是否已設定的示例

以下示例演示了 Java Calendar isSet() 方法的使用。我們使用 getInstance() 方法建立一個當前日期的日曆例項,使用 getTime() 方法列印日期和時間,並檢查欄位是否已設定。下一步,我們清除該欄位,再次列印日期和設定標誌。

package com.tutorialspoint;

import java.util.Calendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = Calendar.getInstance();

      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());
	  
      // determine if the given calendar field has a value set
      System.out.println("Day of month is set: " + cal.isSet(Calendar.DAY_OF_MONTH));
      
      cal.clear(Calendar.DAY_OF_MONTH);
      
      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());
	  
      // determine if the given calendar field has a value set
      System.out.println("Day of month is set: " + cal.isSet(Calendar.DAY_OF_MONTH));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Date And Time Is: Tue Sep 27 10:42:26 IST 2022
Day of month is set: true
Date And Time Is: Tue Sep 27 10:42:26 IST 2022
Day of month is set: false

檢查當前日期 GregorianCalendar 例項中月份天數是否已設定的示例

以下示例演示了 Java Calendar isSet() 方法的使用。我們使用 GregorianCalendar() 方法建立一個當前日期的日曆例項,使用 getTime() 方法列印日期和時間,並檢查欄位是否已設定。下一步,我們清除該欄位,再次列印日期和設定標誌。

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = new GregorianCalendar();

      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());

      // determine if the given calendar field has a value set
      System.out.println("Day of month is set: " + cal.isSet(Calendar.DAY_OF_MONTH));
      
      cal.clear(Calendar.DAY_OF_MONTH);
      
      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());
	  
      // determine if the given calendar field has a value set
      System.out.println("Day of month is set: " + cal.isSet(Calendar.DAY_OF_MONTH));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Date And Time Is: Tue Sep 27 10:43:08 IST 2022
Day of month is set: true
Date And Time Is: Tue Sep 27 10:43:08 IST 2022
Day of month is set: false

檢查給定日期 GregorianCalendar 例項中月份天數是否已設定的示例

以下示例演示了 Java Calendar isSet() 方法的使用。我們使用 GregorianCalendar() 方法建立一個特定日期的日曆例項,使用 getTime() 方法列印日期和時間,並檢查欄位是否已設定。下一步,我們清除該欄位,再次列印日期和設定標誌。

package com.tutorialspoint;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create a calendar    
      Calendar cal = new GregorianCalendar(2025,8,26);

      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());
	  
      // determine if the given calendar field has a value set
      System.out.println("Day of month is set: " + cal.isSet(Calendar.DAY_OF_MONTH));
      
      cal.clear(Calendar.DAY_OF_MONTH);
      
      // print the time
      System.out.println("Date And Time Is: " + cal.getTime());
	  
      // determine if the given calendar field has a value set
      System.out.println("Day of month is set: " + cal.isSet(Calendar.DAY_OF_MONTH));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Date And Time Is: Fri Sep 26 00:00:00 IST 2025
Day of month is set: true
Date And Time Is: Mon Sep 01 00:00:00 IST 2025
Day of month is set: false
java_util_calendar.htm
廣告
© . All rights reserved.