Reputation: 4576
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.
Upvotes: 2
Views: 8535
Reputation: 13
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.
Upvotes: 0
Reputation: 687
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..
Upvotes: 0
Reputation: 6702
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);
}
});
Upvotes: 3
Reputation: 351
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.
Upvotes: 0
Reputation: 2509
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);
Upvotes: 0
Reputation: 187
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?
Upvotes: 0
Reputation: 1667
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 ...
}
Upvotes: 0