0

I want to convert the selected date into the format"dd-MM-yyyy" because c.getTime returns "Sun Mar 3 12:34:46 IST 2013"

private class MyDateListener implements DateListener {

    public void dateChanged(DateEvent e) {

        Calendar c = e.getSelectedDate();
        if (c != null) {
            try {
                SimpleDateFormat parser = new SimpleDateFormat(c.getTime().toString());
                SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                Date date;
                date = parser.parse(date1);
                output = formatter.format(date);
                System.out.println(output);
            } catch (ParseException ex) {
                Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            System.out.println("No time selected.");
        }
    }
}
Muhsina M
  • 21
  • 2
  • 8

1 Answers1

0

Do you want:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(c.getTime());

The getTime() function actually returns a date, which you can pass to your formatter, so there is no need to parse it.

beny23
  • 34,390
  • 5
  • 82
  • 85