Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR,2007);
calendar.set(Calendar.MONTH,2);
calendar.set(Calendar.DAY_OF_MONTH,28);
calendar.add(calendar.DAY_OF_YEAR,3);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)+ "- "+calendar.get(Calendar.MONTH)+ "- "+calendar.get(Calendar.DAY_OF_MONTH));
打印结果是:7-2-31
怎么会是2.31号呢,我哪里写错了?
------解决方案--------------------
打印结果正确,Calendar.MONTH 的范围是0-11。
------解决方案--------------------
其实你打印出来的是3月31号,Month是从0开始计数的,应该改为
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR,2007);
calendar.set(Calendar.MONTH,1);
calendar.set(Calendar.DAY_OF_MONTH,28);
//calendar.add(calendar.DAY_OF_MONTH,3);
calendar.add(5,3);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd ");
System.out.println(simpleDateFormat.format(calendar.getTime()));