Java Calendar using Calendar.DAY_OF_WEEK to get the first and the last dates for a particular date

前端 未结 3 1992
不知归路
不知归路 2021-01-28 19:49

In my application there lies a code which works abruptly sometimes, its about getting a week interval using the java calendar object through Calendar.DAY_OF_WEEK. The code check

3条回答
  •  春和景丽
    2021-01-28 20:13

    The issue is in locale. In English(US), Sunday is the first day of the week. Check this code:

    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        System.out.println("FirstDayOfWeek="+cal.getFirstDayOfWeek());
        System.out.println(cal.getTime().toString());
        cal = Calendar.getInstance(Locale.FRANCE);
        System.out.println("FirstDayOfWeek="+cal.getFirstDayOfWeek());
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        System.out.println(cal.getTime().toString());
    

提交回复
热议问题