The Calendar.isSet() method determines whether a specified calendar field has an assigned value.
Field State Inspection: Returns true if set, or false if reset via clear(). To assign values, see set(). To read fields, see get(). For validation, see isLenient().
Defined directly in java.util.Calendar:
// Formal signature of Calendar.isSet() in java.util.Calendar
public final boolean isSet(int field)
| Parameter | Type | Description & Operational Rule |
|---|---|---|
field |
int |
The calendar field constant to check (e.g. Calendar.YEAR, Calendar.MONTH). |
| Return Value | boolean |
true if the field has an assigned value; false if undefined. |
| Modifiers | final |
Cannot be overridden by subclasses; ensures consistent field flag checking. |
Demonstrating how clear(field) transitions a field from set to unset:
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
// Fresh calendar instance has all fields set
System.out.println("Initial YEAR isSet: " + cal.isSet(Calendar.YEAR)); // true
System.out.println("Initial MONTH isSet: " + cal.isSet(Calendar.MONTH)); // true
// Clear only the YEAR field
cal.clear(Calendar.YEAR);
System.out.println("Cleared YEAR isSet: " + cal.isSet(Calendar.YEAR)); // false
System.out.println("Untouched MONTH: " + cal.isSet(Calendar.MONTH)); // trueTerminal Output:
Initial YEAR isSet: true
Initial MONTH isSet: true
Cleared YEAR isSet: false
Untouched MONTH: true
Calling clear() unsets every field across the calendar:
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
cal.clear();
System.out.println("YEAR: " + cal.isSet(Calendar.YEAR)); // false
System.out.println("DATE: " + cal.isSet(Calendar.DATE)); // falseTerminal Output:
YEAR: false
DATE: false
Accessing the time causes Java to normalize and compute all remaining fields, marking them as set:
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, 2026);
System.out.println("Before: " + cal.isSet(Calendar.DAY_OF_MONTH)); // false
cal.getTime(); // triggers field calculation
System.out.println("After: " + cal.isSet(Calendar.DAY_OF_MONTH)); // trueTerminal Output:
Before: false
After: true
Clearing 12-hour HOUR does not unset 24-hour HOUR_OF_DAY:
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
// Clear 12-hour HOUR field
cal.clear(Calendar.HOUR);
System.out.println("HOUR isSet: " + cal.isSet(Calendar.HOUR)); // false
System.out.println("HOUR_OF_DAY isSet: " + cal.isSet(Calendar.HOUR_OF_DAY)); // trueTerminal Output:
HOUR isSet: false
HOUR_OF_DAY isSet: true
Tracking the boolean return of isSet() through common operations:
| Operation Invoked | Target Field isSet() | Other Fields isSet() |
|---|---|---|
Calendar.getInstance() |
true |
true (initialized to current clock) |
cal.clear() |
false |
false (all fields undefined) |
cal.clear(field) |
false |
true (unaffected) |
cal.set(field, val) |
true |
Preserved until time computation |
Building queries where only a subset of calendar coordinates are supplied:
import java.util.Calendar;
public class PartialDateParser {
public static Calendar build(Integer y, Integer m, Integer d) {
Calendar cal = Calendar.getInstance();
cal.clear();
if (y != null) cal.set(Calendar.YEAR, y);
if (m != null) cal.set(Calendar.MONTH, m);
if (d != null) cal.set(Calendar.DAY_OF_MONTH, d);
return cal;
}
public static void main(String[] args) {
Calendar p = build(2026, Calendar.OCTOBER, null);
System.out.println("Year: " + p.isSet(Calendar.YEAR)); // true
System.out.println("Day: " + p.isSet(Calendar.DAY_OF_MONTH)); // false
}
}Terminal Output:
Year: true
Day: false
Auditing field flags before executing date arithmetic:
import java.util.Calendar;
public class CalendarStateAudit {
public static void printState(Calendar cal) {
System.out.println("Year: " + cal.isSet(Calendar.YEAR) +
" | Date: " + cal.isSet(Calendar.DATE));
}
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.clear(Calendar.DATE);
printState(cal);
}
}Terminal Output:
Year: true | Date: false
In modern Java 8+, field presence is checked through isSupported():
import java.time.LocalDate;
import java.time.temporal.ChronoField;
// Modern Java 8+ TemporalAccessor field support check
LocalDate date = LocalDate.of(2026, 10, 23);
System.out.println("Supports DAY_OF_MONTH: " + date.isSupported(ChronoField.DAY_OF_MONTH)); // true
System.out.println("Supports HOUR_OF_DAY: " + date.isSupported(ChronoField.HOUR_OF_DAY)); // falseTerminal Output:
Supports DAY_OF_MONTH: true
Supports HOUR_OF_DAY: false
Calling cal.get() resolves unset fields, simultaneously flipping isSet() back to true.
Invoking getTime() recalculates all fields, marking them true.
You can compile and run this complete verification class locally in any Java IDE or environment:
import java.util.Calendar;
public class CalendarIsSetFullDemo {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
// 1. Verify fresh instance has fields set
if (cal.isSet(Calendar.YEAR)) {
System.out.println("Check 1 Passed: Initial instance has YEAR set.");
}
// 2. Verify single field clearing
cal.clear(Calendar.MONTH);
if (!cal.isSet(Calendar.MONTH) && cal.isSet(Calendar.YEAR)) {
System.out.println("Check 2 Passed: Single field cleared while others remain set.");
}
// 3. Verify explicit set transitions back to true
cal.set(Calendar.MONTH, Calendar.JULY);
if (cal.isSet(Calendar.MONTH)) {
System.out.println("Check 3 Passed: set() marks field as set.");
}
System.out.println("All Calendar.isSet() verification checks passed!");
}
}Terminal Output:
Check 1 Passed: Initial instance has YEAR set.
Check 2 Passed: Single field cleared while others remain set.
Check 3 Passed: set() marks field as set.
All Calendar.isSet() verification checks passed!
Returns true for all fields because it is populated with current system clock values.
Call cal.clear(field) to unset a specific field, or cal.clear() to unset all fields.
Any call to get() or getTime() calculates missing fields, causing isSet() to transition back to true.
Use TemporalAccessor.isSupported(ChronoField).
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.