import java.util.Calendar; import java.util.Date; /** * @author ZheShao * @version 1.0 * @description: TODO * @date 2024/1/26 15:32 */ public class CalendarDemo01 { public static void main(String[] args) { //1. Calendar是一个抽象类 不能直接new 而是通过一个静态方法获取到子类对象 //会把时间的纪元,年,月,日,时,分,秒,星期等放在一个数组中 //月份:范围0~11 如果获取出来的是0,那么实际上是一月 //星期:星期日为一周中的第一天 // 1(星期日) 2 3 4 5 6 7 /* 0 :纪元 1: 年 2: 月 3: 一年中的第几周 4: 一个月中的第几周 5: 一个月中第几天(日期) */ Calendar c=Calendar.getInstance(); System.out.println(c); //2.修改日历的时间 Date d=new Date(0L); c.setTime(d); System.out.println(c); c.set(Calendar.YEAR,2000); c.set(Calendar.MONTH,11); c.set(Calendar.DATE,10); //3.得到日期 int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH)+1; int week=c.get(Calendar.DAY_OF_WEEK); int day=c.get(Calendar.DAY_OF_MONTH); System.out.println(year+","+month+","+day+","+getweek(week)); //4.添加时间 c.add(Calendar.YEAR,1); c.add(Calendar.MONTH,1); c.add(Calendar.DATE,2); int year1=c.get(Calendar.YEAR); int month1=c.get(Calendar.MONTH)+1; int week1=c.get(Calendar.DAY_OF_WEEK); int day1=c.get(Calendar.DAY_OF_MONTH); System.out.println(year1+","+month1+","+day1+","+getweek(week1)); } //查表法 public static String getweek(int index){ //1~7 星期日~星期六 String []arr={"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; return arr[index]; } }
Calendar类
![](https://img-home.csdnimg.cn/images/20240711042549.png)