Calendar days have values 1-7 for days Sunday-Saturday. getFirstDayOfWeek
returns one of this values (usually of Monday or Sunday) depending on used Locale
. Calendar.getInstance
uses default Locale
depening on phone's settings, which in your case has Monday as first day of the week.
One solution would be to use other Locale
:
Calendar.getInstance(Locale.US).getFirstDayOfWeek()
would return 1
, which is value of Calendar.SUNDAY
Other solution would be to use chosen day of week value like
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
Problem is, Calendar
is using its inner first day of the week value in set
as well. Example:
Calendar mondayFirst = Calendar.getInstance(Locale.GERMANY); //Locale that has Monday as first day of week
mondayFirst.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
log(DateUtils.formatDateTime(context, mondayFirst.getTimeInMillis(), 0));
//prints "May 19" when runned on May 13
Calendar sundayFirst = Calendar.getInstance(Locale.US); //Locale that has Sunday as first day of week
sundayFirst.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
log(DateUtils.formatDateTime(context, sundayFirst.getTimeInMillis(), 0));
//prints "May 12" when runned on May 13
If you don't want to use Locale
or you need other day as the first day of the week, it may be best to calculate start of the week on your own.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…