The Calendar.clear() method sets calendar field values and time representations to an undefined state.
Field Reset & Midnight Normalization: Calling clear() causes isSet() to return false. It eliminates residual hours, minutes, and milliseconds, ensuring accurate equals() and before() checks. To set values, see set(); to read values, see get().
Defined directly in java.util.Calendar:
// 2 Overloaded signatures in java.util.Calendar
public final void clear()
public final void clear(int field)
| Signature | Scope of Reset | Description & Operational Rule |
|---|---|---|
clear() |
All Calendar Fields | Sets every calendar field and the millisecond time value undefined. |
clear(int field) |
Specific Field Constant | Sets the designated field (e.g. Calendar.YEAR) undefined. |
Calling clear() resets all fields, causing isSet() to return false:
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
// Displays current system time
System.out.println("Before clear(): " + cal.isSet(Calendar.YEAR)); // true
// Clear all fields: resets state to undefined
cal.clear();
System.out.println("After clear(): " + cal.isSet(Calendar.YEAR)); // false
System.out.println("Default epoch: " + cal.getTimeInMillis()); // 0 (Jan 1, 1970 00:00:00 GMT)Terminal Output:
Before clear(): true
After clear(): false
Default epoch: 0
Targeting a single field constant clears only that component while leaving other fields intact:
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
// Check initial status
System.out.println("Year is set: " + cal.isSet(Calendar.YEAR)); // true
System.out.println("Month is set: " + cal.isSet(Calendar.MONTH)); // true
// Clear only the YEAR field
cal.clear(Calendar.YEAR);
System.out.println("Year is set: " + cal.isSet(Calendar.YEAR)); // false
System.out.println("Month is set: " + cal.isSet(Calendar.MONTH)); // true (unaffected)Terminal Output:
Year is set: true
Month is set: true
Year is set: false
Month is set: true
Zeroing out time fields eliminates residual milliseconds for exact date-only comparisons:
import java.util.Calendar;
Calendar cal = Calendar.getInstance();
// Zero-out all time components while preserving current date
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println("Hour: " + cal.get(Calendar.HOUR_OF_DAY)); // 0
System.out.println("Minute: " + cal.get(Calendar.MINUTE)); // 0
System.out.println("Second: " + cal.get(Calendar.SECOND)); // 0Terminal Output:
Hour: 0
Minute: 0
Second: 0
When all fields are cleared, Java computes date representations relative to the Unix Epoch (January 1, 1970 00:00:00 UTC):
import java.util.Calendar;
import java.util.TimeZone;
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.clear(); // Complete reset
// Epoch time formatted in UTC
System.out.println("Epoch Year: " + cal.get(Calendar.YEAR)); // 1970
System.out.println("Epoch Month: " + cal.get(Calendar.MONTH)); // 0 (January)
System.out.println("Epoch Date: " + cal.get(Calendar.DATE)); // 1Terminal Output:
Epoch Year: 1970
Epoch Month: 0
Epoch Date: 1
Comparing field reset strategies across different development requirements:
| Operation | isSet() Result | Time Value Impact | Recommended Usage |
|---|---|---|---|
cal.clear() |
false for all fields |
Resets epoch to 0 | Complete initialization before setting date-only values. |
cal.clear(field) |
false for target field |
Recalculates from remaining | Clearing specific transient fields. |
cal.set(field, 0) |
true |
Sets value explicitly to 0 | Zeroing hour, minute, second for midnight normalization. |
Constructing standardized calendar instances at exact midnight to ensure accurate equals() evaluation:
import java.util.Calendar;
public class DateOnlyNormalizer {
public static Calendar createMidnightDate(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.clear(); // remove residual hours, minutes, and ms
cal.set(year, month, day);
return cal;
}
public static void main(String[] args) {
Calendar d1 = createMidnightDate(2026, Calendar.OCTOBER, 23);
Calendar d2 = createMidnightDate(2026, Calendar.OCTOBER, 23);
System.out.println("Identical: " + d1.equals(d2)); // true
}
}Terminal Output:
Identical: true
Creating clean calendar objects without inherited system timestamps:
import java.util.Calendar;
public class CleanCalendarFactory {
public static Calendar getCleanCalendar() {
Calendar cal = Calendar.getInstance();
cal.clear();
return cal;
}
public static void main(String[] args) {
Calendar fresh = getCleanCalendar();
System.out.println("Any field set? " + fresh.isSet(Calendar.DAY_OF_MONTH)); // false
}
}Terminal Output:
Any field set? false
Calendar.clear() has a void return type. To inspect whether a field has been assigned a value, invoke cal.isSet(field).
Java differentiates between 12-hour (HOUR) and 24-hour (HOUR_OF_DAY) fields. Clearing one does not clear the other. To reset time fully, clear all time constants or call clear().
You can compile and run this complete verification class locally in any Java IDE or environment:
import java.util.Calendar;
public class CalendarClearFullDemo {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
// 1. Verify single field clearing
cal.clear(Calendar.YEAR);
if (!cal.isSet(Calendar.YEAR) && cal.isSet(Calendar.MONTH)) {
System.out.println("Check 1 Passed: Single field cleared while others remain set.");
}
// 2. Verify all fields cleared
cal.clear();
if (!cal.isSet(Calendar.MONTH) && !cal.isSet(Calendar.DATE)) {
System.out.println("Check 2 Passed: All fields reset to undefined state.");
}
// 3. Verify clean midnight construction
cal.set(2026, Calendar.JANUARY, 1);
if (cal.get(Calendar.HOUR_OF_DAY) == 0 && cal.get(Calendar.MILLISECOND) == 0) {
System.out.println("Check 3 Passed: Cleared calendar initialized cleanly at midnight.");
}
System.out.println("All Calendar.clear() verification checks passed!");
}
}Terminal Output:
Check 1 Passed: Single field cleared while others remain set.
Check 2 Passed: All fields reset to undefined state.
Check 3 Passed: Cleared calendar initialized cleanly at midnight.
All Calendar.clear() verification checks passed!
It means the field has not been explicitly computed or set. isSet() returns false until a value is assigned or resolved.
Calendar.getInstance() initializes with the current system millisecond. Calling clear() prevents residual time components from corrupting date math.
Cleared fields resolve to Unix Epoch: January 1, 1970 00:00:00 UTC.
Modern classes like LocalDate.of(2026, 10, 23) have no time components, eliminating the need for clearing.
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.