Java中的Calendar類是一個(gè)抽象類,用于處理日期和時(shí)間。下面是使用Calendar類的一些常見操作:
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
System.out.println(date);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2022);
calendar.set(Calendar.MONTH, Calendar.JANUARY); // 月份從0開始,所以1代表2月
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // 月份從0開始,所以0代表1月
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1); // 增加一天
calendar.add(Calendar.MONTH, -1); // 減少一個(gè)月
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2022, Calendar.JANUARY, 1);
calendar2.set(2022, Calendar.FEBRUARY, 1);
boolean isAfter = calendar1.after(calendar2); // 判斷calendar1是否在calendar2之后
boolean isBefore = calendar1.before(calendar2); // 判斷calendar1是否在calendar2之前
這些是Calendar類的一些基本用法,可以根據(jù)具體需求進(jìn)行調(diào)整和擴(kuò)展。