Search code examples
javadateoopcaldroid

Caldroid calendar unparsable date exception


I am trying to get the selected date in dd/MM/yyyy format from Caldroid calendar but when I am parsing the date it will throw an following error.

java.text.ParseException: Unparseable date: "Thu May 04 00:00:00 GMT 2017" (at offset 0)

Below is my code:-

final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
final CaldroidListener caldroidListener = new CaldroidListener() {
            @Override
            public void onSelectDate(Date date, View view) {
                try {
                    String dateParse = date.toString();
                    Log.d("Date before parse: ",date.toString());
                    sdf.parse(dateParse);
                    Log.d("Date after parse: ",dateParse);
                    eventDate.setText(dateParse);
                }catch(ParseException e){
                    e.printStackTrace();
                }
            }
        };

Solution

  • This does what you want your code to do (without the logging):

    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    final CaldroidListener caldroidListener = new CaldroidListener() {
                @Override
                public void onSelectDate(Date date, View view) {
                    eventDate.setText(sdf.format(date));
    
                }
            };
    

    You use SimpleDateFormat#format to get a formatted string out of your Date.