我想要获取当前month.That中的周数,包括上个月最后一周的天数和下个月的第一周天数。
如下所示:
我想在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)];
}
发布于 2016-05-26 14:30:30
我们可以使用calendar类,然后执行如下操作
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));
这将返回当月的周数
发布于 2013-01-22 17:34:18
使用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));
}
}
结果:
# 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
发布于 2013-11-12 21:41:26
如果你想用“手”来做这件事
使用以下命令:
Date.prototype.getMonthWeek = function(monthAdjustement)
{
var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
var returnMessage = (Math.ceil(this.getDate()/7) + Math.floor(((7-firstDay)/7)));
return returnMessage;
}
其中,monthAdjustement
只是一个整数,用于调整您查找的月份与今天日期的比较。
https://stackoverflow.com/questions/14454736
复制相似问题