0

I use Android materialCalendar view library in one of my app,the user can select a particular date from the calendar view,i need to get the list of week days of that particular selected date .. i use the following code which retreives the current week days

daysList = new ArrayList<>();
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("dd");
    cal.setTime(day.getDate());

    for (int i = 0; i < 7; i++) {
        Log.i("dateTag", sdf.format(cal.getTime()));
        daysList.add(sdf.format(cal.getTime()));
        cal.add(Calendar.DAY_OF_WEEK, 1);

    }
    return daysList;

when i try to change the 3rd line of code to the user selected date, i get error as the selected date is type of materialcalendar view,how do i convert this selected date to java.util date

where day.getDate() is the user selected date from the widget...

ashif-ismail
  • 1,037
  • 17
  • 34
  • Where is the code getting the user selected date. What's the type of this selected date? Where is the documentation of the library? Assuming you're talking about [this](https://prolificinteractive.github.io/material-calendarview/com/prolificinteractive/materialcalendarview/OnDateSelectedListener.html), the listener is called with a CalendarDay, which has a getDate() method. – JB Nizet Oct 30 '16 at 15:05
  • Ya,the same library...how to use it in set method – ashif-ismail Oct 30 '16 at 15:06
  • Post your code, and answer my questions first. Then explain clearly what you're trying to achieve, what you expect to happen and what happens instead. – JB Nizet Oct 30 '16 at 15:08
  • ya,i use the same library u specified,i'm getting the user selected date using the getSelectedDate() method in the library.what i need is to get the list of week days for that selected date.with the code i have posted above i'm getting the list of current week days...i need to modify it to get the list of week days for the selected date by the user..how do i do this ... – ashif-ismail Oct 30 '16 at 15:17
  • see the updated question for my code – ashif-ismail Oct 30 '16 at 15:18
  • So, you're trying to call Calendar.set(), which expects a calendar field (like DATE, MONTH or YEAR), of type int (see https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int,%20int)), with `day.getDate()`, which I assume is of type java.util.Date. That can't possibly work. Asking for the list of week days for a given date doesn't make much sense to me. A date has a single week day: Monday, or Tuesday, or whatever, but not several. And your title says "How to Convert Material Calendar View Selected date to java.util Date type", but it seems you know how to do that. – JB Nizet Oct 30 '16 at 15:28
  • So, once again, post all the relevant code, and explain, clearly, what you want to achieve. Like if I was a little child: when the user selects a date in the UI, the following should happen: ... – JB Nizet Oct 30 '16 at 15:29
  • OK,this is my scenario....I have the material calendar widget from which user can select a date.I have an activity with 7 tabs.initially i set current week days as title of each tabs.suppose if u were to see my app now.the tab would be like this 30,31,1,2,3,4,5.when the user selects any date from widget I need to update the tab title with the week days with respective to the user selected date...does this make sense?pls help..any clarifications needed? – ashif-ismail Oct 30 '16 at 15:34
  • So, get the selected Date, create a Calendar and set its time (https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTime(java.util.Date)), then use a loop to get the day of week of the calendar, incrementing the date at each iteration. – JB Nizet Oct 30 '16 at 15:43
  • Can u pls post a code snippet,or make some changes to my code if possible – ashif-ismail Oct 30 '16 at 15:45
  • 1
    You said you were able to get the days of weeks for the current date. The code would be strictly identical, except you would have to call cal.setTime(selectedDate) to start from the selected date rather than the current date. You should be able to do that by yourself. – JB Nizet Oct 30 '16 at 15:48
  • OK,thanks..I'll try and let u know – ashif-ismail Oct 30 '16 at 15:53
  • It would be great helpful if u post snippet though...please – ashif-ismail Oct 30 '16 at 15:55
  • 1
    Take time to figure things out by yourself. To read documentation. That's how you learn things and become autonomous. It boils down to a single line of code to add to your original code (the one in the first version of your question), and I gave you this line of code. – JB Nizet Oct 30 '16 at 15:59
  • Sure,I'll try and let u knwo – ashif-ismail Oct 30 '16 at 16:56
  • check my updated code in the question,now i'm getting the next seven days from the selected date..but this is not actually what i need..i need to get the list of days of a particular calendar row in which the selected day falls..suppose if u were to look calendar today and user selects 31,i need to get list as 30,31,1,2,3,4,5... – ashif-ismail Oct 31 '16 at 04:37

0 Answers0