内容:日历对象获取时间,设置时间,日期偏移
通过工厂化获得对象。
getInstance();
get() 获取时间信息
Calendar c = Calendar.getInstance(); //获取其中的日期 int year = c.get(Calendar.YEAR) ; int month = c.get(Calendar.MONTH) + 1; int day = c.get(Calendar.DAY_OF_MONTH) ; int week = c.get(Calendar.DAY_OF_WEEK) ; System.out.println(year + "年" + month + "月" + day + "日" + week);
美国的星期是从星期天开始的,所以会有点问题
set()设置方法,设置时间
//日期的偏移 c.add(Calendar.YEAR,4);
//解答三月一日的前一天就是 Calendar c = Calendar.getInstance(); int year = 2012; c.set(year,2,1); //设置年月日 System.out.println(c.get(Calendar.YEAR)+" " + (c.get(Calendar.MONTH)+1) + " " + c.get(Calendar.DAY_OF_MONTH)); c.add(Calendar.DAY_OF_MONTH,-1); //日减1,月会自动减 System.out.println(c.get(Calendar.YEAR)+" " + (c.get(Calendar.MONTH)+1) + " " + c.get(Calendar.DAY_OF_MONTH));