如何用Java在6个月内修改日期

Ran*_*hit 3 java datetime date

我希望在Java中增加6个月,目前我在代码下面使用.但它总是在第一个月印刷.你能告诉我这里的错误吗?我是Java的初学者.

这是我的输出:

现行日期:2013年1月11日

6个月后的日期:2013年7月11日

预期产量:

当前日期:2013年5月11日

6个月后的日期:2013年11月11日

        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));
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 11

尝试使用大写M:

DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); 
Run Code Online (Sandbox Code Playgroud)

  • +1正是我认为错误也是如此,你可以说目前他的月被评估为分钟. (2认同)