Javaで月を6ずつ増やしたいのですが、現在以下のコードを使用しています。ただし、常に最初の月に印刷されます。ここで私が間違っていることを教えてください。私はJavaの初心者です。
これは私の出力です:
現在の日付 : 2013 年 11 月 1 日
6 か月後の日付: 2013 年 11 月 7 日
期待される出力:
現在の日付: 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));