Calendar class
Calendar: It provides some methods for the conversion between a specific moment and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc., and provides some methods for manipulating calendar fields (for example, getting the date of the next week).
One, construction method
protected Calendar(): Since the modifier is protected, the object cannot be created directly. The object needs to be generated by other means.
2. Member method
Member methods of the Calendar class
static Calendar getInstance() | Use the default time zone and locale to get the calendar. Generate Calendar object by this method. As shown below: Calendar cr=Calendar.getInstance(); |
---|---|
public void set(int year,int month,int date,int hourofday,int minute,int second) | Set the year, month, day, hour, minute, and second of the calendar. |
public int get(int field) | Returns the value of the given calendar field. The so-called fields are year, month, day, etc. |
public void setTime(Date date) | Use the given Date to set the time of this calendar. Date------Calendar |
public Date getTime() | Returns a Date representing the time of this calendar. Calendar-----Date |
abstract void add(int field,int amount) | According to the rules of the calendar, add or reduce the amount of time to the specified field. |
public long getTimeInMillies() | Returns the time value of this calendar in milliseconds. |
Third, the calendar field
There are two types of calendar fields: one is a unit of time, such as year, month, day, and so on. The other is a specific date, such as January, February, March, the first, the second, the third, one o'clock, two o'clock, and so on. The former is used for general acquisition, and the latter is used for general judgment.
Time unit field:
YEAR | year | MINUTE | Minute | DAY_OF_WEEK_IN_MONTH | The week of the month |
---|---|---|---|---|---|
MONTH | month | SECOND/MILLISECOND | Seconds/millisecond | WEEK_OF_MONTH | Calendar week |
DATE | day | DAY_OF_MONTH | Same as DATE | DAY_OF_YEAR | The day of the year |
HOUR_OF_DAY | Time | DAY_OF_WEEK | which day | WEEK_OF_YEAR | Week of the year |
Specific time field: It is not cumbersome here, from January to December, from Monday to Friday
Special: AM_PM returns 1 to indicate afternoon, and returns 0 to indicate morning.
4. DEMO
public class CalendarDemo {
public static void main(String[] args) {
// 其日历字段已由当前日期和时间初始化:
Calendar rightNow = Calendar.getInstance(); // 子类对象
// 获取年
int year = rightNow.get(Calendar.YEAR);
// 获取月
int month = rightNow.get(Calendar.MONTH);
// 获取日
int date = rightNow.get(Calendar.DATE);
//获取几点
int hour=rightNow.get(Calendar.HOUR_OF_DAY);
//获取上午下午
int moa=rightNow.get(Calendar.AM_PM);
if(moa==1)
System.out.println("下午");
else
System.out.println("上午");
System.out.println(year + "年" + (month + 1) + "月" + date + "日"+hour+"时");
rightNow.add(Calendar.YEAR,5);
rightNow.add(Calendar.DATE, -10);
int year1 = rightNow.get(Calendar.YEAR);
int date1 = rightNow.get(Calendar.DATE);
System.out.println(year1 + "年" + (month + 1) + "月" + date1 + "日"+hour+"时");
}
}
Note: month starts from 0, and month starts from 1, so month needs to be increased by one.