5

I have date picker in my application and its works well .But I need selected day of week instead of date (i.e., if I select the date 23 I need to get day like "friday" instead of number). I used OnDateSetListener for click event.

    private DatePickerDialog.OnDateSetListener datePickerListener = new         DatePickerDialog.OnDateSetListener() 
{       
    public void onDateSet(DatePicker view, int selectedYear,
        int selectedMonth, int selectedDay) 
    {   

               }
       }

I need day in string(like monday(or) tuesday..) for selectedDay in above code

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
shivcena
  • 2,023
  • 8
  • 24
  • 50
  • Possible duplicate of [Android: how to get the current day of the week (Monday, etc...) in the user's language?](http://stackoverflow.com/questions/7651221/android-how-to-get-the-current-day-of-the-week-monday-etc-in-the-users-l) – bummi Jan 28 '16 at 14:57

4 Answers4

11

change your code as for getting day of week using SimpleDateFormat :

private DatePickerDialog.OnDateSetListener 
     datePickerListener = new  DatePickerDialog.OnDateSetListener() 
{       
    public void onDateSet(DatePicker view, int selectedYear,
        int selectedMonth, int selectedDay) 
    {   
      SimpleDateFormat simpledateformat = new SimpleDateFormat("EEEE");
      Date date = new Date(selectedYear, selectedMonth, selectedDay-1);
      String dayOfWeek = simpledateformat.format(date);
    }
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • @pros: i got the day of week in string format , but it gets previous day(such as saturday for friday,sunday for saturday). i use selectedDay-1 instead of selectedDay to get correct result.pls change it to mark as correct. – shivcena Dec 20 '12 at 07:17
1

This will give u the day names::

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
            Date d_name = new Date();
            String dayOfTheWeek = sdf.format(d_name);
mainu
  • 448
  • 2
  • 11
1

Don't use:

Date date = new Date(selectedYear, selectedMonth, selectedDay-1);

It is deprecated and will give wrong output in some cases. Check with 28-jan-2015 and 04-jul-1940, both are Thursday but it returns Friday for 04-jul-1940.

Use:

GregorianCalendar GregorianCalendar = new GregorianCalendar(year, monthOfYear, dayOfMonth-1);

int dayOfWeek=date.get(date.DAY_OF_WEEK); 

The above method will return an integer value:

  • 1 - Monday
  • 2 - Tuesday
  • ...
  • 7 - Sunday
Zenadix
  • 15,291
  • 4
  • 26
  • 41
seshu
  • 11
  • 1
1

Use this


GregorianCalendar gregorianCalendar = new GregorianCalendar(year, month,dayOfMonth-1);
int week_number=gregorianCalendar.get(Calendar.DAY_OF_WEEK);

You can use switch case to get week day in words.