Calendar为抽象类所以不可以实例化,需要通过 Calendar.getInstance()获取。
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
//设置日期和时间
calendar.set(2012, 10, 22);
//设置小时
calendar.set(Calendar.HOUR, 10);
//设置分
calendar.set(Calendar.MINUTE, 4);
//设置秒
calendar.set(Calendar.SECOND, 54);
calendar.add(Calendar.DATE, 10); //意思是把当前日期加上10天
calendar.add(Calendar.HOUR_OF_DAY, 10);//意思是把当前时间加上10个小时
System.out.println("+++++" + calendar.getTimeInMillis());
//输出年份
System.out.println("+++++++++++++" + calendar.get(Calendar.YEAR));
//输出上下午
System.out.println("+++++++++++++++++++++" + calendar.get(Calendar.PM));
//输出月份
System.out.println("+++++++++++++++++++++" + (calendar.get(Calendar.MONTH) + 1));
//输出日
System.out.println("+++++++++++++++++++++" + calendar.get(Calendar.DATE));
//输出时
System.out.println("+++++++++++++++++++++" + calendar.get(Calendar.HOUR));
//输出分
System.out.println("+++++++++++++++++++++" + calendar.get(Calendar.MINUTE));
//输出秒
System.out.println("+++++++++++++++++++++" + calendar.get(Calendar.SECOND));
// 星期,英语国家星期从星期日开始计算.输出星期几
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
求某个月的最后一天 // 假设求6月的最后一天 int currentMonth = 6; // 先求出7月份的第一天,实际中这里6为外部传递进来的currentMonth变量 // 1 calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1); calendar.add(Calendar.DATE, -1); // 获取日 int day = calendar.get(Calendar.DAY_OF_MONTH); System.out.println("6月份的最后一天为" + day + "号");