How to advance the modification date by 6 months in Java

See the English answer > java: Customize adding 1 month to the current date

This is my output:

Current date: January 11, 2013

Date after 6 months: July 11, 2013

Expected production:

Current date: May 11, 2013

Date after 6 months: November 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));

Solution

Try capital M:

DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>