How to get day of the week from CalendarView

Akeshwar Jha picture Akeshwar Jha · Mar 7, 2016 · Viewed 7k times · Source

I'm having a CalendarView in my layouts and I've added the Date change listener to it:

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

calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

        Log.i("View.getDate", view.getDate() + "");
    }
});

I can access the selected date's year, month and day-of-the-month. But how do I get the day of the week? I want that information for any date that I select in the CalendarView.

I put view.getDate() in the logs, only to know it's giving me the same output (value for today's date) on every date that I select. What am I missing?

UPDATE:

calendar.set(year, month, dayOfMonth);

correctly sets the calendar value correctly, while the following:

calendar.setTimeInMillis(view.getDate()); 

doesn't. Don't know the reason.

Answer

yital9 picture yital9 · Mar 7, 2016

For example:

calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, dayOfMonth);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
     }
});