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

Android Calendar: Changing the start day of week(Android 日历:更改一周的开始日期)
本文介绍了Android 日历:更改一周的开始日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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 开发的初学者.这是我的代码SimpleDateFormat dateformate = new 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());

使用上面的代码我得到了结果,但星期一是一周的明星.

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 根据使用的 Locale 返回其中一个值(通常是星期一或星期日).Calendar.getInstance 使用默认的 Locale 取决于手机的设置,在您的情况下,星期一是一周的第一天.

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.

一种解决方案是使用其他Locale:

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

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

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

如果您不想使用 Locale 或者您需要其他日期作为一周的第一天,最好自己计算一周的开始时间.

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 日历:更改一周的开始日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)