-2

I am trying to produce a list based on a date. I keep getting a Parse exception. I thought I had the dates in the exact same format. Maybe I missed something. I have played with this for hours with no success. Any help would be appreciated.

public static List<String> getMonthlyDates(Date startdate, Date enddate) {
    List<String> dates = new ArrayList<String>();
    Calendar calendar = new GregorianCalendar();


    calendar.setTime(startdate);

    while (calendar.getTime().before(enddate)) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String result = df.format(calendar.getTime());
        dates.add(result);
        calendar.add(Calendar.MONTH, 1);
    }
    return dates;
}

Calendar Mcalendar = Calendar.getInstance();
String dd = editbillduedate.getText().toString();
     //Which shows correctly in the format of 2015-Jun-15 
SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd"); 
     //it makes no difference if format is yyyy-mm-dd
Date date;
        try {
 //THIS LINE BELOW IS WHERE I AM GETTING THE ERROR
            date = format.parse(dd);
            Mcalendar.add(Calendar.DAY_OF_YEAR, 365);
            final Date newDate2 = Mcalendar.getTime();

            List<String> dateList = getMonthlyDates(date, newDate2);

            for (final String d1 : dateList) {
            //Does something irrelevant

            }
            } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
SmulianJulian
  • 801
  • 11
  • 33

1 Answers1

1

If you want to parse dates in the same format like 2015-Jun-15, you should change your SimpleDateFormat to have THREE characters for the month element like this: yyyy-MMM-dd. So change this :

SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd"); 

to:

SimpleDateFormat  format = new SimpleDateFormat("yyyy-MMM-dd"); 

and this:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

to:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • Okay, Okay. Maybe I did did deserve a couple of downvotes. Now I get it. MM is numerical month, right. MMM is first three letters of month. The EditText was in MMM format and I was asking for MM. Gotcha...wow. I can't believe I spent 2 hours on something that should have taken 5 seconds to figure out. Thanks. – SmulianJulian Jun 15 '15 at 11:54
  • :D Yes, sometimes it happens this way :) You're welcome :) And try to not care about the downvotes, because the haters are everywhere.... – Gabriella Angelova Jun 15 '15 at 11:56