Java Calendar.isSet(): Inspecting Field Assignment State

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().

1. Syntax & Method Signature 🔝

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.

2. Basic State Inspection: Before and After clear() 🔝

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));  // true

Terminal Output:

Initial YEAR isSet:  true
Initial MONTH isSet: true
Cleared YEAR isSet:  false
Untouched MONTH:     true

3. Global Reset Behavior: clear() on All Fields 🔝

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));  // false

Terminal Output:

YEAR: false
DATE: false

4. Lazy Recalculation Impact: How getTime() Re-Sets Fields 🔝

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));  // true

Terminal Output:

Before: false
After:  true

5. Field Independence: HOUR vs. HOUR_OF_DAY 🔝

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));  // true

Terminal Output:

HOUR isSet:        false
HOUR_OF_DAY isSet: true

6. Comparison: Field State Lifecycle Across Calendar Methods 🔝

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

7. Production Recipe: Partial Date Construction Helper 🔝

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

8. Practical Recipe: Calendar Field State Auditor 🔝

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

9. Modern Java 8+ Comparison: TemporalAccessor.isSupported() 🔝

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));   // false

Terminal Output:

Supports DAY_OF_MONTH: true
Supports HOUR_OF_DAY:  false

10. Common Developer Pitfalls & Traps 🔝

Assuming isSet() Prevents Defaulting

Calling cal.get() resolves unset fields, simultaneously flipping isSet() back to true.

Checking isSet() After getTime()

Invoking getTime() recalculates all fields, marking them true.

11. Complete Compilable Java Program 🔝

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!

12. Frequently Asked Questions (FAQ) 🔝

What does isSet() return on a fresh Calendar.getInstance()?

Returns true for all fields because it is populated with current system clock values.

How do I make isSet() return false?

Call cal.clear(field) to unset a specific field, or cal.clear() to unset all fields.

Why did isSet() return true after I cleared it?

Any call to get() or getTime() calculates missing fields, causing isSet() to transition back to true.

What is the modern Java 8+ equivalent?

Use TemporalAccessor.isSupported(ChronoField).

Further Reading & Related Tutorials 🔝


Java



plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer