简体   繁体   中英

Android Calendar: Changing the start day of week

i have a little problem, i'm developing an application, and i need to change the start day of the week from monday to another one (thursday, of saturday). is this possible in android, i need to calculate the start to week and its end knowing the date. (the week starts ano thursday as example)

Note: i'm just a beginner in android development. here is my code SimpleDateFormat dateformate = new SimpleDateFormat("dd/MM");

// get today and clear time of day
Calendar cal = Calendar.getInstance();

// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
cal.add(Calendar.DAY_OF_YEAR, 7*(WeekIndex-1));
result = dateformate.format(cal.getTime());

cal.add(Calendar.DAY_OF_YEAR, 6 );

result=result+" - " + dateformate.format(cal.getTime());

using the above code im getting the result but with monday as the star of week.

Note: i can't add day to the result because week index changes with the changing of it's start

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.

GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 0);

changing the value 0 - starts day from monday changing the value 1 - starts day from sunday and so on..

hope this helps and works :)

    public int getWeekdayOfMonth(int year, int month){
    Calendar cal = Calendar.getInstance();
    cal.set(year, month-1, 1);
    dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
    return dayOfWeek;
}

weekday = getWeekdayOfMonth();

int day = (weekday - firstweek) < 0 ? (7 - (firstweek - weekday)) : (weekday - firstweek);

"firstweek" means what the start day of you want

then you can calculate the first day you should show.If you have simple method,please tell us. thks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM