Java Android 日历:更改一周的开始日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16522425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:16:38  来源:igfitidea点击:

Android Calendar: Changing the start day of week

javaandroidcalendar

提问by Mouhamad Lamaa

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)

我有一个小问题,我正在开发一个应用程序,我需要将一周的开始日期从星期一更改为另一个(星期四,星期六)。这在android中是否可行,我需要计算一周的开始和知道日期的结束。(例如,一周从星期四开始)

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

注意:我只是 android 开发的初学者。这是我的代码 SimpleDateFormat dateformat = 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

注意:我无法在结果中添加日期,因为周索引会随着开始的变化而变化

采纳答案by Koger

Calendar days have values 1-7 for days Sunday-Saturday. getFirstDayOfWeekreturns one of this values (usually of Monday or Sunday) depending on used Locale. Calendar.getInstanceuses default Localedepening on phone's settings, which in your case has Monday as first day of the week.

日历日的值为 1-7,表示周日至周六的天数。getFirstDayOfWeek根据 used 返回此值之一(通常是星期一或星期日)LocaleCalendar.getInstance使用默认Locale取决于手机的设置,在您的情况下,星期一作为一周的第一天。

One solution would be to use other Locale:

一种解决方案是使用 other Locale

Calendar.getInstance(Locale.US).getFirstDayOfWeek()

would return 1, which is value of Calendar.SUNDAY

将返回1,这是值Calendar.SUNDAY

Other solution would be to use chosen day of week value like

其他解决方案是使用选定的星期几值,如

cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

Problem is, Calendaris using its inner first day of the week value in setas well. Example:

问题是,Calendar也在使用其内部一周的第一天值set。例子:

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 Localeor you need other day as the first day of the week, it may be best to calculate start of the week on your own.

如果您不想使用Locale或需要另一天作为一周的第一天,最好自己计算一周的开始时间。

回答by AlexChu

    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();

工作日 = getWeekdayOfMonth();

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

"firstweek" means what the start day of you want

“firstweek”表示你想要的开始日

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

那么你可以计算你应该展示的第一天。如果你有简单的方法,请告诉我们。谢谢

回答by Rakesh Kumar B Badiger

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..

更改值 0 - 从星期一开始一天 更改值 1 - 从星期日开始一天等等..

hope this helps and works :)

希望这会有所帮助并有效:)