2

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.

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91

7 Answers7

3

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);
     }
});
yital9
  • 6,544
  • 15
  • 41
  • 54
  • That's where the problem is. `view.getDate` is returning me the same result - no matter what date I select. – Akeshwar Jha Mar 07 '16 at 11:35
  • Thankyou. I don't know why `view.getDate` is returning the same value. Any thoughts? `calendar.set(year, month, dayOfMonth)` sets the calendar value properly. – Akeshwar Jha Mar 07 '16 at 11:53
0

Use the Android Calendar class.

Calendar selected = Calendar.getInstance();
selected.setTimeInMillis(view.getDate());
int dayOfWeek = selected.get(Calendar.DAY_OF_WEEK);

switch (dayOfWeek ) {
    case Calendar.SUNDAY:
        // ...

    case Calendar.MONDAY:
        // etc ...
}
Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44
0

this should not be difficult, for example does this answer your question? Android: how to get the current day of the week (Monday, etc...) in the user's language?

Community
  • 1
  • 1
Jimmy
  • 187
  • 7
0

Try with this use the Calendar and set it through the time you get from the calendar view:

    Calendar selected = Calendar.getInstance();
    selected.setTimeInMillis(calendarView .getDate());
    int dayOfWeek = selected.get(Calendar.DAY_OF_WEEK);
Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37
  • Hi, thanks for the response. But I'm getting the same value for `view.getDate` no matter what date I select. – Akeshwar Jha Mar 07 '16 at 11:37
  • 1
    hmm if you set year, month and day to Calendar which values can get from onSelectedDayChange selected.set(year,month,day); and remove selected.setTimeInMillis(calendarView .getDate()); did this help ? – Kristiyan Varbanov Mar 07 '16 at 11:45
  • Yup, setting year, month and day does the thing, but why not `getDate`? Any idea? – Akeshwar Jha Mar 07 '16 at 11:48
0
 public static int dayOfWeek(String date) {
    try {
        int day = 0;
        SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = simpleDateformat.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        day = calendar.get(Calendar.DAY_OF_WEEK);
        return day;
    } catch (Exception e) {
        return 0;
    }
}

Then you can use Calendar.MONDAY, Calendar.TUESDAY etc.

Hope this is helpful.

António Paulo
  • 351
  • 3
  • 18
0

use Calender class from java api.

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() + "");
        /*
             for month you can use following. like
              0 for jan
              1 for feb

        */
         int newMonth;
         if(month > 9){
           newMonth= month-1;
         }
        else{
            switch(month){
              case (1|01):{
                 newMonth = 0;
              }
              break;
              case (2|02):{
                 newMonth = 1;
              }
              break;
             ....
             }
         }
            Calendar calendar = new      GregorianCalendar(year, newMonth
, dayOfMonth);
            int result = calendar.get(Calendar.DAY_OF_WEEK);
        switch (result) {
        case Calendar.MONDAY:
            System.out.println("It's Monday !");
            break;
    }
    }
});

I hope, This will help you..

rockstar
  • 587
  • 4
  • 22
0

Too late but well, I had the same problem. I solved it with this.

    public static int dayOfWeek(int day, int month, int year) {

    Calendar c = Calendar.getInstance();

    c.set(year,month,day);

    return c.get(Calendar.DAY_OF_WEEK);
}

This method return the day of the week in an int. If the day is Sunday it return 1 and if it's Wednesday return 4.