java.util.Calendar Class in Java
java.util.Calendar Class
The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
The class also provides additional fields and methods for implementing a concrete calendar system outside the package. Those fields and methods are defined as protected.
Creating Objects
Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:
Calendar rightNow = Calendar.getInstance();
A Calendar object can produce all the calendar field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
java.util.Calendar Class Fields
public static final int AM
Value of the AM_PM field indicating the period of the day from midnight to just before noon.
public static final int PM
Value of the AM_PM field indicating the period of the day from noon to just before midnight.
public static final int SHORT
A style specifier for getDisplayName and getDisplayNames equivalent to SHORT_FORMAT.
public static final int LONG
A style specifier for getDisplayName and getDisplayNames equivalent to LONG_FORMAT.
public static final int YEAR
Field number for get and set indicating the year.
public static final int MONTH
Field number for get and set indicating the month.
public static final int WEEK_OF_YEAR
Field number for get and set indicating the week number within the current year.
public static final int WEEK_OF_MONTH
Field number for get and set indicating the week number within the current month.
public static final int DATE
Field number for get and set indicating the day of the month. This is a synonym for DAY_OF_MONTH. The first day of the month has value 1.
public static final int DAY_OF_MONTH
Field number for get and set indicating the day of the month. This is a synonym for DATE. The first day of the month has value 1.
public static final int DAY_OF_YEAR
Field number for get and set indicating the day number within the current year. The first day of the year has value 1.
public static final int DAY_OF_WEEK
Field number for get and set indicating the day of the week.
public static final int DAY_OF_WEEK_IN_MONTH
Field number for get and set indicating the ordinal number of the day of the week within the current month.
public static final int HOUR
Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.
public static final int HOUR_OF_DAY
Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.
public static final int MINUTE
Field number for get and set indicating the minute within the hour. E.g., at 10:04:15.250 PM the MINUTE is 4.
public static final int SECOND
Field number for get and set indicating the second within the minute. E.g., at 10:04:15.250 PM the SECOND is 15.
public static final int MILLISECOND
Field number for get and set indicating the millisecond within the second. E.g., at 10:04:15.250 PM the MILLISECOND is 250.
public static final int SUNDAY
Value of the DAY_OF_WEEK field indicating Sunday.
public static final int MONDAY
Value of the DAY_OF_WEEK field indicating Monday.
public static final int TUESDAY
Value of the DAY_OF_WEEK field indicating Tuesday.
public static final int WEDNESDAY
Value of the DAY_OF_WEEK field indicating Wednesday.
public static final int THURSDAY
Value of the DAY_OF_WEEK field indicating Thursday.
public static final int FRIDAY
Value of the DAY_OF_WEEK field indicating Friday.
public static final int SATURDAY
Value of the DAY_OF_WEEK field indicating Saturday.
public static final int JANUARY
Value of the MONTH field indicating the first month of the year in the Gregorian and Julian calendars.
public static final int FEBRUARY
Value of the MONTH field indicating the second month of the year in the Gregorian and Julian calendars.
public static final int MARCH
Value of the MONTH field indicating the third month of the year in the Gregorian and Julian calendars.
public static final int APRIL
Value of the MONTH field indicating the fourth month of the year in the Gregorian and Julian calendars.
public static final int MAY
Value of the MONTH field indicating the fifth month of the year in the Gregorian and Julian calendars.
public static final int JULY
Value of the MONTH field indicating the seventh month of the year in the Gregorian and Julian calendars.
public static final int AUGUST
Value of the MONTH field indicating the eighth month of the year in the Gregorian and Julian calendars.
public static final int SEPTEMBER
Value of the MONTH field indicating the ninth month of the year in the Gregorian and Julian calendars.
public static final int OCTOBER
Value of the MONTH field indicating the tenth month of the year in the Gregorian and Julian calendars.
public static final int NOVEMBER
Value of the MONTH field indicating the eleventh month of the year in the Gregorian and Julian calendars.
public static final int DECEMBER
Value of the MONTH field indicating the twelfth month of the year in the Gregorian and Julian calendars.
java.util.Calendar Class Methods
public boolean after(Object when)
Returns whether this Calendar represents a time after the time represented by the specified Object. This method is equivalent to: compareTo(when) > 0 if and only if when is a Calendar instance. Otherwise, the method returns false.
public boolean before(Object when)
Returns whether this Calendar represents a time before the time represented by the specified Object. This method is equivalent to: compareTo(when) < 0 if and only if when is a Calendar instance. Otherwise, the method returns false.
public int compareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
Returns:the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than 0 if the time of this Calendar is after the time represented by the argument.
public final Instant toInstant()
Converts this object to an Instant.
The conversion creates an Instant that represents the same point on the time-line as this Calendar.
java.util.Calendar Classs Example
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
// Creating a Calendar instance
Calendar calendar = Calendar.getInstance();
// Getting current date and time
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // Months are 0-based, so adding 1
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24-hour format
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
// Displaying current date and time
System.out.println("Current Date and Time:");
System.out.printf("%04d-%02d-%02d %02d:%02d:%02d%n", year, month, dayOfMonth, hour, minute, second);
// Adding 1 day to the current date
calendar.add(Calendar.DAY_OF_MONTH, 1);
// Getting the date after adding 1 day
int newYear = calendar.get(Calendar.YEAR);
int newMonth = calendar.get(Calendar.MONTH) + 1;
int newDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
// Displaying the date after adding 1 day
System.out.println("\nDate after adding 1 day:");
System.out.printf("%04d-%02d-%02d%n", newYear, newMonth, newDayOfMonth);
// Setting a specific date
calendar.set(2024, Calendar.JULY, 7); // July 7, 2024
// Getting day of the week for the set date
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Displaying the day of the week
System.out.println("\nDay of the week for July 7, 2024:");
System.out.println(daysOfWeek[dayOfWeek - 1]); // Adjusting index for array
// Clearing all fields of the calendar
calendar.clear();
// Displaying cleared calendar date (default date)
System.out.println("\nCleared Calendar Date (Default):");
System.out.printf("%tF%n", calendar.getTime());
}
}
Output:
Current Date and Time: 2024-07-07 16:26:49
Date after adding 1 day: 2024-07-08
Day of the week for July 7, 2024: Sunday
Cleared Calendar Date (Default): 1970-01-01