0

I want to add 2 Months in another Date but he only add 60 Days in Date and there is no increment in Months of the Date as well as in Year. I'm using following code for adding Date in another Date. Days adding correctly but there is no increment in Month of Date. if I add 60 Days then he add but again there is no increment in Month as well as in Year. If someone help me to resolve problem then I really thank full!!!

String dob = "06/05/2021";
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Calendar c = Calendar.getInstance();

try {
    c.setTime(sdf.parse(dob));
} catch (ParseException e) {
    e.printStackTrace();
}

c.add(Calendar.DATE, 60);
c.add(Calendar.MONTH,2); // Not Working
sdf = new SimpleDateFormat("dd/mm/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
String incToDate = sdf.format(resultdate);
Toast.makeText(this, incToDate, Toast.LENGTH_SHORT).show();
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 5
    `mm` is for minutes. Use `MM` for months – Reimeus May 10 '21 at 10:44
  • Yes, the real answer is: you have to know what the characters mean that you put in your code. Meaning: the javadoc tells you explicitly what these chars in the formatting pattern mean. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html ... dont just use things you "found" somewhere. Research then, **understand** where they are coming from, and what they *mean*. – GhostCat May 10 '21 at 11:11

1 Answers1

2

Your pattern is not correct, it basically reads two digits for day of month/two digits for minute of hour/4 digits for year. I'm guessing you did not want any minutes of hour in this pattern, so change the lower-case ones to upper-case ones.

If you can use java.time, you could get the desired result like this:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) {
        // provide an example date as String
        String dob = "06/05/2021";
        
        // create a formatter that can parse a String in the given format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/uuuu");
        
        // parse the String to a LocalDate using the previously defined formatter
        LocalDate localDob = LocalDate.parse(dob, dtf);
        
        // print the (formatted) result just to see if parsing has worked
        System.out.println("Just parsed " + localDob.format(dtf));
        
        // add two months and print a result phrase
        LocalDate localDobTwoMonthsLater = localDob.plusMonths(2);
        System.out.println("Adding two months results in "
                            + localDobTwoMonthsLater.format(dtf));
        
        // for completeness, add 60 days and print the result
        LocalDate localDobSixtyDaysLater = localDob.plusDays(60);
        System.out.println("Adding sixty days results in "
                            + localDobSixtyDaysLater.format(dtf));
    }
}

This code prints

Just parsed 06/05/2021
Adding two months results in 06/07/2021
Adding sixty days results in 05/07/2021
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    @GhostCat The exact words don't really matter... The point is what matters ;-) – deHaar May 10 '21 at 11:50
  • 1
    Thanks Bro it working fine after changing only format of code in my own code it's working fine. Once again Thank you – Muhammad May 11 '21 at 07:51
  • @Muhammad You're welcome! Please consider accepting and upvoting my answer if it was helpful to you. Thanks! – deHaar May 11 '21 at 07:53