datepicker selected day of the week android

后端 未结 3 1625
太阳男子
太阳男子 2021-01-13 04:20

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\" ins

相关标签:
3条回答
  • 2021-01-13 05:06

    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
    0 讨论(0)
  • 2021-01-13 05:08

    This will give u the day names::

    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
                Date d_name = new Date();
                String dayOfTheWeek = sdf.format(d_name);
    
    0 讨论(0)
  • 2021-01-13 05:14

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题