Java Calendar.get(): Extracting Date & Time Field Values

The Calendar.get() method returns the integer value of a specified calendar field.

Field Extraction & Temporal Inspection: Used to read year, month, date, hour, minute, and weekday fields. To set fields, see set(). For date math, see add(). For maximum bounds in a month, see getActualMaximum(), or get month names with getDisplayName().

1. Syntax & Method Signature 🔝

Defined in java.util.Calendar:

 // Formal signature of Calendar.get() in java.util.Calendar
public int get(int field)
Parameter Type Description & Operational Rule
field int The calendar field constant to retrieve (e.g. Calendar.YEAR, Calendar.MONTH).
Return Value int The integer value associated with the specified calendar field.
Normalization Automatic Cascades pending field updates in lenient mode.

2. Extracting Core Date Fields (Year, Month, Day) 🔝

Reading date coordinates from an initialized instance:

import java.util.Calendar;

Calendar cal = Calendar.getInstance();
cal.set(2026, 5, 25, 22, 59);

System.out.println("YEAR:         " + cal.get(Calendar.YEAR));          // 2026
System.out.println("MONTH:        " + cal.get(Calendar.MONTH));         // 5 (June)
System.out.println("DAY_OF_MONTH: " + cal.get(Calendar.DAY_OF_MONTH));  // 25

Terminal Output:

YEAR:         2026
MONTH:        5
DAY_OF_MONTH: 25

3. Extracting Time Fields: 12-Hour vs. 24-Hour Formats 🔝

Differentiating between HOUR (0-11) and HOUR_OF_DAY (0-23):

import java.util.Calendar;

Calendar cal = Calendar.getInstance();
cal.set(2026, Calendar.OCTOBER, 23, 22, 15);

System.out.println("HOUR (12-hr):        " + cal.get(Calendar.HOUR));         // 10
System.out.println("HOUR_OF_DAY (24-hr): " + cal.get(Calendar.HOUR_OF_DAY));  // 22
System.out.println("AM_PM (0=AM, 1=PM):  " + cal.get(Calendar.AM_PM));        // 1 (PM)
System.out.println("MINUTE:              " + cal.get(Calendar.MINUTE));       // 15

Terminal Output:

HOUR (12-hr):        10
HOUR_OF_DAY (24-hr): 22
AM_PM (0=AM, 1=PM):  1
MINUTE:              15

4. Reading the Day of the Week 🔝

DAY_OF_WEEK returns 1 (Sunday) through 7 (Saturday):

import java.util.Calendar;

Calendar cal = Calendar.getInstance();
cal.set(2026, Calendar.OCTOBER, 23);

int dow = cal.get(Calendar.DAY_OF_WEEK);
System.out.println("DAY_OF_WEEK: " + dow);  // 6

boolean isWeekend = (dow == Calendar.SATURDAY || dow == Calendar.SUNDAY);
System.out.println("Is Weekend:  " + isWeekend);  // false

Terminal Output:

DAY_OF_WEEK: 6
Is Weekend:  false

5. The Field Value vs. Field Name Constant Trap 🔝

Constants like Calendar.SUNDAY are field values, not field names. Passing them to get() reads field indices:

import java.util.Calendar;

Calendar cal = Calendar.getInstance();
cal.set(2026, 5, 25);

 // TRAP: SUNDAY is a value (1), so cal.get(1) reads Calendar.YEAR!
System.out.println("cal.get(Calendar.SUNDAY): " + cal.get(Calendar.SUNDAY));  // 2026!

 // CORRECT: Pass DAY_OF_WEEK and compare with SUNDAY
System.out.println("Is Sunday: " + (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY));  // false

Terminal Output:

cal.get(Calendar.SUNDAY): 2026
Is Sunday: false

6. Production Recipe: Custom Timestamp Formatter 🔝

Extracting calendar fields to build standardized ISO-style date strings:

import java.util.Calendar;

public class CustomTimestampFormatter {
    public static String formatIso(Calendar cal) {
        return String.format("%04d-%02d-%02d %02d:%02d",
            cal.get(Calendar.YEAR),
            cal.get(Calendar.MONTH) + 1,
            cal.get(Calendar.DAY_OF_MONTH),
            cal.get(Calendar.HOUR_OF_DAY),
            cal.get(Calendar.MINUTE));
    }

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println("Formatted: " + formatIso(cal));
    }
}

Terminal Output:

Formatted: 2026-10-23 09:05

7. Practical Recipe: Business Hours & Weekend Checker 🔝

Combining DAY_OF_WEEK and HOUR_OF_DAY for business hours:

import java.util.Calendar;

public class BusinessHoursChecker {
    public static boolean isOpen(Calendar cal) {
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        if (dow == Calendar.SATURDAY || dow == Calendar.SUNDAY) return false;
        int h = cal.get(Calendar.HOUR_OF_DAY);
        return h >= 9 && h < 17;
    }

    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        System.out.println("Open: " + isOpen(now));
    }
}

Terminal Output:

Open: true

8. Comparison: Calendar.get() vs. java.time Getters 🔝

Contrasting legacy constants with modern getters:

Target Field Legacy Calendar.get() Modern java.time Equivalent
Month Value cal.get(Calendar.MONTH) (0-11) ldt.getMonthValue() (1-12)
Day of Month cal.get(Calendar.DAY_OF_MONTH) (1-31) ldt.getDayOfMonth() (1-31)
Hour (24-hr) cal.get(Calendar.HOUR_OF_DAY) (0-23) ldt.getHour() (0-23)

9. Modern Java 8+ Comparison: Calendar vs. java.time 🔝

Comparing legacy constants with modern getters:

import java.time.LocalDateTime;

LocalDateTime ldt = LocalDateTime.of(2026, 10, 23, 14, 30);
System.out.println("Year:  " + ldt.getYear());
System.out.println("Month: " + ldt.getMonthValue());  // 1-12 directly!

Terminal Output:

Year:  2026
Month: 10

10. Common Developer Pitfalls & Traps 🔝

Zero-Indexed Month Offset

cal.get(Calendar.MONTH) returns 0 (January) to 11 (December). Always add 1 for human displays: cal.get(Calendar.MONTH) + 1.

Confusing HOUR with HOUR_OF_DAY

Calendar.HOUR wraps 0-11. Reading HOUR at 8 PM returns 8. For 24-hour military time, use Calendar.HOUR_OF_DAY.

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 CalendarGetFullDemo {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(2026, Calendar.DECEMBER, 25, 18, 30);

        if (cal.get(Calendar.YEAR) == 2026) {
            System.out.println("Check 1 Passed: Year verified.");
        }
        if (cal.get(Calendar.HOUR_OF_DAY) == 18) {
            System.out.println("Check 2 Passed: 24-hour verified.");
        }
        if (cal.get(Calendar.AM_PM) == Calendar.PM) {
            System.out.println("Check 3 Passed: PM verified.");
        }
        System.out.println("All Calendar.get() verification checks passed!");
    }
}

Terminal Output:

Check 1 Passed: Year verified.
Check 2 Passed: 24-hour verified.
Check 3 Passed: PM verified.
All Calendar.get() verification checks passed!

12. Frequently Asked Questions (FAQ) 🔝

Why does Calendar.MONTH start at 0 instead of 1?

Legacy C conventions indexed months 0-11. Modern Java 8+ LocalDate uses 1-12.

What is the difference between DATE and DAY_OF_MONTH?

They are exact synonyms in Java, pointing to the same underlying field constant.

How do I get the textual name of a month or day?

Use cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US).

What does cal.get(Calendar.DAY_OF_YEAR) return?

Returns ordinal day of year (1-365, or 366 in leap years).

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