Nez*_*zam 17 java android calendar
我希望获得当月的周数.包括上个月的上周和下个月的第一周.
像这样的东西:
简单不是吗?http://i50.tinypic.com/5ml2de.png
我想在android上的某个gridview适配器中使用它,并且我试图操纵这个代码块:
//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)];
}
Run Code Online (Sandbox Code Playgroud)
Ven*_*oid 10
Calendar类的WEEK_OF_YEAR属性对您有用.参考:日历类
创建一个新日期,该日期将是某个月的第一天.得到这一天的一周,假设你有起始价值.
创建一个新日期,该日期将是给定月份的最后一天.得到这一天的一周,所以现在你有了最终价值.
现在结束 - 开始+ 1应该是你想要的.
当一周重叠到另一年或类似的时候,您可能必须处理一些极端情况.但是我认为一旦你做对了,你就可以用简单的逻辑做到这一点.
这是简单的示例代码.我认为你可以把它变成功能并传递你想要的任何东西.
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 ));
Run Code Online (Sandbox Code Playgroud)
对于角落案例:
转角情况仅发生在Januaray月(例如2010年1月,2000年1月),在那些情况下,大多数日子是上一年的最后一周的一部分,所以起始值将是52,结束值将是5.这发生检查,如果,
if(start > end) {
numweeks = end + 1;
}
Run Code Online (Sandbox Code Playgroud)
PS:把它放到你得到的不同测试输入上.让我知道如果你发现任何错误,我可以改进它.
甚至更简单的解决方案:
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
Run Code Online (Sandbox Code Playgroud)
简单的解决方案适用于角落案例:
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);
}
Run Code Online (Sandbox Code Playgroud)
日志:
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
Run Code Online (Sandbox Code Playgroud)
小智 7
使用WEEK_OF_MONTH会更好,没有跨年问题.
public static void main(String[] args) throws Exception {
for (String m : new String[] { "2012-11", "2012-12", "2013-01", "2013-02", "2013-03",
"2013-04", "2013-05", "2013-06" }) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Date date = format.parse(m);
Calendar c = Calendar.getInstance();
c.setTime(date);
int start = c.get(Calendar.WEEK_OF_MONTH);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
int end = c.get(Calendar.WEEK_OF_MONTH);
System.out.println(" # of weeks in " + format.format(c.getTime())
+ ": " + (end - start + 1));
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
# of weeks in 2012-11: 5
# of weeks in 2012-12: 6
# of weeks in 2013-01: 5
# of weeks in 2013-02: 5
# of weeks in 2013-03: 6
# of weeks in 2013-04: 5
# of weeks in 2013-05: 5
# of weeks in 2013-06: 6
Run Code Online (Sandbox Code Playgroud)
我们可以使用日历类,然后执行类似的操作
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));
Run Code Online (Sandbox Code Playgroud)
这将返回当月的周数