Java Calendar Class: Full Usage Guide
The Java Calendar class in Java is used for managing dates and times. It offers multiple methods for accessing, setting, and manipulating date and time information.
The Calendar class is an abstract class that cannot be directly instantiated, but can be obtained by calling its static method getInstance(). By default, the getInstance() method returns a Calendar object representing the current date and time.
The Calendar class offers numerous methods for handling dates and times, including the following commonly used methods:
- get() is used to retrieve the value of a specific field, such as year, month, day, hour, minute, or second.
- set(): used to assign values to specific fields, such as year, month, day, hour, minute, second, etc.
- add(): used to increase or decrease a specified value on a specified field, such as adding a day or subtracting an hour.
- roll(): Similar to the add() method, but only increases or decreases the specified field without affecting other fields.
- getTime(): Used to obtain a Date object representing the date and time of a Calendar object.
- setTime(): Used to set a Date object representing a date and time to the date and time represented by a Calendar object.
In addition to the methods mentioned above, the Calendar class also offers other methods such as setting and obtaining the first day of the week, as well as getting the maximum and minimum values of specific fields.
It is important to note that in the Calendar, the month field starts at 0, with 0 representing January and 11 representing December. Additionally, the week field in the Calendar starts at 1, with 1 representing Sunday, 2 representing Monday, and so on.
Here is an example code:
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
// 获取当前日期和时间的Calendar对象
Calendar calendar = Calendar.getInstance();
// 获取年份
int year = calendar.get(Calendar.YEAR);
System.out.println("Year: " + year);
// 获取月份(注意月份是从0开始的)
int month = calendar.get(Calendar.MONTH) + 1;
System.out.println("Month: " + month);
// 获取日期
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("Day: " + day);
// 设置日期为2022年10月1日
calendar.set(Calendar.YEAR, 2022);
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("New Date: " + calendar.getTime());
// 在日期上增加一天
calendar.add(Calendar.DAY_OF_MONTH, 1);
System.out.println("New Date: " + calendar.getTime());
// 获取一周的第一天(默认为星期日)
int firstDayOfWeek = calendar.getFirstDayOfWeek();
System.out.println("First Day of Week: " + firstDayOfWeek);
// 获取星期几(1表示星期日,2表示星期一,以此类推)
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("Day of Week: " + dayOfWeek);
// 获取指定字段的最大值和最小值
int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int minDayOfMonth = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println("Max Day of Month: " + maxDayOfMonth);
System.out.println("Min Day of Month: " + minDayOfMonth);
}
}
The output is as follows:
Year: 2021
Month: 10
Day: 22
New Date: Sat Oct 01 00:00:00 CST 2022
New Date: Sun Oct 02 00:00:00 CST 2022
First Day of Week: 1
Day of Week: 1
Max Day of Month: 31
Min Day of Month: 1
In the above code, first a Calendar object representing the current date and time is obtained using the Calendar.getInstance() method. Then, the get() method is used to retrieve information such as the year, month, and date, and the set() method is used to set the date to October 1, 2022. Next, the add() method is used to add a day to the date, the getFirstDayOfWeek() method is used to retrieve the first day of the week, the get() method is used to get the day of the week, and the getActualMaximum() and getActualMinimum() methods are used to get the maximum and minimum values of a specified field. Finally, the getTime() method is used to convert the Calendar object.