我想获得特定月份的周数
我想获取当前月份的周数.包括上周的上个月的天数和下个月的第一周天数.
i want to get number of weeks in current month.That including the previous month's days in the last week and the first week days in the next month.
类似这样的:
我想在 android 上的某个 gridview 适配器中使用它,并试图操纵这个代码块:
I want to use this in a certain gridview adapter on android and am trying to manipulate this code block:
//FIRST_DAY_OF_WEEK =0;
int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDay = (int) month.get(Calendar.DAY_OF_WEEK);
int firstDayNextMonth = nextMonth.get(Calendar.DAY_OF_WEEK);
int lastDayNextMonth=nextMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
int nextMonthdays=nextMonth.getMinimalDaysInFirstWeek();
// figure size of the array
if (firstDay == 1) {
days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6)];
}
else {
days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1)];
}
推荐答案
Calendar 类的 WEEK_OF_YEAR 属性对你很有用.参考:日历类
The WEEK_OF_YEAR attribute of the Calendar class can be useful for you. Ref: Calendar class
创建一个新日期,该日期将是某个月份的第一天.获取这一天的一年中的一周,假设您获得了起始值.
Create a new date that will be the first day of the some month. Get the week of the year for this day, let say you got start value.
创建一个新日期,该日期将是给定月份的最后一天.获取这一天的一年中的一周,所以现在你得到了最终值.
Create a new date that will be the last day of the given month. Get the week of the year for this day, so now you got end value.
现在 end - start + 1 应该是你想要的.
Now end - start + 1 should be one that you want.
当一周与另一年或类似的时间重叠时,您可能需要处理一些极端情况.但我认为,一旦你做对了,你就可以用简单的逻辑做到这一点.
You may have to handle some corner case when the week overlaps to another year or similar. But I think once you get this right, you can do that with simple logic.
这是简单的示例代码.我认为你可以让它变成函数并传递你想要的任何东西.
Here is simple example code. I think you can make it into function and pass whatever you want.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int start = cal.get(Calendar.WEEK_OF_YEAR);
Log.d("BLA BLA", "Value is " + start);
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 28);
int end = cal.get(Calendar.WEEK_OF_YEAR);
//Above line will not work for December Month, use following line for this
int end = isDecember?53:cal.get(Calendar.WEEK_OF_YEAR);
Log.d("BLA BLA", "Value is " + end);
Log.d("BLA BLA", "Num weeks:: " + (end - start +1 ));
转角案例:
极端情况只会发生在 Januaray 月份(例如 2010 年 1 月、2000 年 1 月),在这些情况下,大部分时间都是上一年最后一周的一部分,因此开始值将是 52,结束值将是5. 发生这种情况时检查是否,
Corner case will only occur for the month of Januaray (E.g Jan 2010, Jan 2000) in those cases most of the days are part of the last week of the previous year so the start value will be 52 and end value will be 5. When this occurs check if,
if(start > end) {
numweeks = end + 1;
}
P.S:把它放到你得到的不同的测试输入中.如果您发现任何错误,请告诉我是否可以改进.
P.S: Put it to different testing inputs that you got. Let me know If I can improve on it once you find any fault.
甚至更简单的解决方案:
Calendar cal = Calendar.getInstance();
for(int i = 0 ; i < 11;i++){
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, i);
int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
Log.d("LOG","max week number" + maxWeeknumber);
}
01-22 01:49:03.591: D/LOG(573): max week number5
01-22 01:49:03.602: D/LOG(573): max week number5
01-22 01:49:03.602: D/LOG(573): max week number6
01-22 01:49:03.671: D/LOG(573): max week number5
01-22 01:49:03.671: D/LOG(573): max week number5
01-22 01:49:03.671: D/LOG(573): max week number6
01-22 01:49:03.681: D/LOG(573): max week number5
01-22 01:49:03.691: D/LOG(573): max week number5
01-22 01:49:03.691: D/LOG(573): max week number5
01-22 01:49:03.711: D/LOG(573): max week number5
01-22 01:49:03.711: D/LOG(573): max week number5
简单的解决方案适用于极端情况:
Calendar cal = Calendar.getInstance();
for(int i = 0 ; i < 11;i++){
cal.set(Calendar.YEAR, 2010);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, i);
int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
// Month value starts from 0 to 11 for Jan to Dec
Log.d("LOG","For Month :: "+ i + " Num Week :: " + maxWeeknumber);
}
日志:
01-22 01:59:35.841: D/LOG(629): For Month :: 0 Num Week :: 6
01-22 01:59:35.841: D/LOG(629): For Month :: 1 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 2 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 3 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 4 Num Week :: 6
01-22 01:59:35.852: D/LOG(629): For Month :: 5 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 6 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 7 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 8 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 9 Num Week :: 6
01-22 01:59:35.871: D/LOG(629): For Month :: 10 Num Week :: 5
相关文章