datepicker selected day of the week android

后端 未结 3 1628
太阳男子
太阳男子 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

提交回复
热议问题