Android的日历:更改星期开始日 [英] Android Calendar: Changing the start day of week
问题描述
我有一个小问题,我开发一个应用程序,我需要一周的开始日从周一改到另一个(星期六周四)。这是可能在Android中, 我需要计算开始到本周及其最终知道日期。 (周开始ANO周四为例)
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开发只是一个初学者。 这里是我的code SimpleDateFormat的dateformate =新的SimpleDateFormat(DD / MM);
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());
使用上述code即时得到结果,但与周一作为本周的明星。
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
推荐答案
日历天有日(星期日)至周六值1-7。 getFirstDayOfWeek
返回这个值(通常是周一或周日)之一,根据所使用区域设置
。 Calendar.getInstance
使用默认的区域设置
depening在手机的设置,而你的情况有星期一作为一周的第一天。
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()
将返回 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, 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.
这篇关于Android的日历:更改星期开始日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!