![]() |
VOOZH | about |
The Calendar class in Java represents and manipulates date and time using fields such as YEAR, MONTH, DAY, and HOUR. It is an abstract class that extends Object and implements Comparable, Serializable, and Cloneable, so it cannot be instantiated using a constructor.
This example demonstrates creating a Calendar object using Calendar.getInstance() and retrieving the current date and time.
Current date and time: Fri Dec 19 06:28:56 UTC 2025
Explanation:
| METHOD | DESCRIPTION |
|---|---|
| add(int field, int amount) | Adds or subtracts a specified amount of time from a calendar field. |
| int get(int field) | Returns the value of a specific calendar field. |
| getMaximum(int field) | Returns the maximum valid value for a calendar field. |
| getMinimum(int field) | Returns the minimum valid value for a calendar field. |
| Date getTime() | Returns a Date object representing calendar time |
Below examples illustrate the above methods:
This example shows how to modify dates using the add() method of the Calendar class.
15 days ago: Thu Dec 04 07:31:57 UTC 2025 4 months later: Sat Apr 04 07:31:57 UTC 2026 2 years later: Tue Apr 04 07:31:57 UTC 2028
Explanation:
This example retrieves specific date and time fields from the current calendar using the Calendar class.
Year: 2025 Date: 19 Minute: 18 Second: 42
Explanation:
This example shows how to find the maximum possible values of specific calendar fields using the Calendar class.
Days in a week: 7 Weeks in a year: 53
Explanation:
This example shows how to find the minimum valid values of calendar fields using the getMinimum() method.
Min days in week: 1 Min weeks in year: 1
Explanation:
Note: Calendar belongs to the legacy date-time API. For new applications, the java.time package (such as LocalDate and LocalDateTime) is recommended.