Calculate Expiry Date in java [closed]

Get the date of manufacturing (as String) as input from the user. Also get the number of months of validity of that product.

Calculate the expiry date by adding that months to the date of manufacturing and display the result.

Assume the given date is a valid date and the format is dd/MM/yyyy

Sample Input 1


Sample Output 1


pseudo code which I tried but unable to implement getting many errors:

  1. Parse the input string to Date (say Date d) using SimpleDateFormat

  2. Use Calendar to add months

        Calendar c = Calendar.getInstannce();  //Get Instance
        c.setTime(dd1);   // Set date to Calendar
        c.add(Calendar.MONTH, month);  //add month value to the Calendar
  3. Convert Calendar to Date using getTime

          Date dd=c.getTime();
  4. Again use the same SimpleDateFormat and convert date to String of the format dd/MM/yyyy using the format method


Well… something like this:

String originalString = "15/12/2020";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(originalString);

Calculate the expiry date using java.calendar by adding that months to the date of manufacturing

    public static java.util.Date addMonths(java.util.Date date ,
            int monthsToAdd) {
        if (today != null) {
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.add(Calendar.MONTH, monthsToAdd);
            return c.getTime();
        } else {
            return null;