0

I have this code written to convert a string which i extracted from a webpage into a date format. But whenever I run it, it converts the month into 01.

Can you please help me debug this code?

    if (this.subject != null && !this.subject.isEmpty()) {
        DateFormat userDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date df = DATE_FORMAT.parse(this.subject);
        Calendar cal = Calendar.getInstance();
        cal.setTime(df);
        String theDate = String.format("%tY-%tm-%td", cal, cal, cal);
        return theDate;
    }
user3645276
  • 51
  • 2
  • 2
  • 5

2 Answers2

1

You do not need GregorianCalendar (I hope for you). Juste use this:

if (this.subject != null && !this.subject.isEmpty()) {
    DateFormat userDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    return userDateFormat.parse(this.subject);
}
lpratlong
  • 1,421
  • 9
  • 17
1

Try this code This should help

if (this.subject != null && !this.subject.isEmpty()) {
    DateFormat userDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    DateFormat outDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    Date df = userDateFormat.parsethis.subject );
    return outDateFormat.format(df);
}
Jens
  • 67,715
  • 15
  • 98
  • 113