How to get date from calendarView() onCreate, with a specific format e.g. DD/MM/YYYY?

Ilja picture Ilja · Mar 17, 2014 · Viewed 13.8k times · Source

I'm trying to get a date from calendar view:

calendar = (CalendarView) findViewById(R.id.calendarView);

by trying:

String selectedDate = calendar.getDate().toString();

But am getting error saying that toString() method can not be resolved I would like to further get it in a format of "DD/MM/YYYY"

Answer

Luca Sepe picture Luca Sepe · Mar 17, 2014

You should use SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String selectedDate = sdf.format(new Date(calendar.getDate()));