<分区>
我想在 Java 中将月份增加 6,目前我正在使用以下代码。但它总是打印第一个月。你能告诉我我在这里做错了什么吗?我是 Java 初学者。
这是我的输出:
当前日期:11-1-2013
6 个月后的日期:11-7-2013
预期输出:
当前日期:11-05-2013
6 个月后的日期:11-11-2013
String dt = "11-05-2013";
DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
Date date = null;
try {
date = (Date)formatter.parse(dt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar now = Calendar.getInstance();
now.setTime(date);
System.out.println("Current date : " + now.get(Calendar.DATE)+ "-" +(now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.YEAR));
now.add(Calendar.MONTH, 6);
System.out.println("date after 6 months : " + now.get(Calendar.DATE)+"-" + (now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.YEAR));