public static void main(String[] args) throws ParseException {
/*
*
*/
String dateFormat = "yyyy-mm-dd";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date date = sdf.parse("2015-12-01");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, 3);
System.out.println(calendar.getTime());
calendar.add(Calendar.MONTH, 3);
System.out.println(calendar.getTime());
calendar.add(Calendar.DAY_OF_YEAR, 10);
calendar.set(Calendar.DAY_OF_WEEK, 3);
System.out.println(calendar.getTime());
}
测试了一下:
把格式变一下就可以,改成如下
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse("20151201");
具体原因估计得看源码
应该变为2019年才对啊
@侯熙: 是变成了2019啊
我的输出
Sat Dec 01 00:00:00 CST 2018
Fri Mar 01 00:00:00 CST 2019
Tue Mar 12 00:00:00 CST 2019
@谦谦王子: 嗯对的,是我写错了
试试在date上增加月份,然后再设置到Calendar
楼上测试得没有问题。
String dateFormat = "yyyy-mm-dd";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date = sdf.parse("2015-12-01");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, 3);
System.out.println("year:"+calendar.getTime());
calendar.add(Calendar.MONTH, 15);
System.out.println("month:"+calendar.getTime());
calendar.add(Calendar.DAY_OF_YEAR, 10);
calendar.set(Calendar.DAY_OF_WEEK, 3);
System.out.println("day:"+calendar.getTime());
--------------------------------------------------------------
结果:
year:Mon Jan 01 00:12:00 CST 2018
month:Mon Apr 01 00:12:00 CST 2019
day:Tue Apr 09 00:12:00 CST 2019
"yyyy-mm-dd" 应该是MM