Get the number of days in a month
public static int daysInMonth(GregorianCalendar c) {
int [] daysInMonths = {31,28,31,30,31,30,31,31,30,31,30,31};
daysInMonths[1] += c.isLeapYear(c.get(GregorianCalendar.YEAR)) ? 1 : 0;
return daysInMonths[c.get(GregorianCalendar.MONTH)];
}
Actually, the Calendar class provides a method to that very simply. For a given Calendar or GregorianCalendar object : calObject.getActualMaximum(calobject.DAY_OF_MONTH)
In the Java API documentation there is a note saying that The version (getActualMaximum()) of this function on Calendar uses an iterative algorithm to determine the actual maximum value for the field. There is almost always a more efficient way to accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar overrides this function with a more efficient implementation. So it looks like it's a lot more efficient to call getActualMaximum from a GregorianCalendar object than a Calendar object. (Thanks to P. Harris for the tip) gregCalObject.getActualMaximum(gregCalObject.DAY_OF_MONTH)
public static int daysInMonth(GregorianCalendar c) {
int [] daysInMonths = {31,28,31,30,31,30,31,31,30,31,30,31};
daysInMonths[1] += c.isLeapYear(c.get(GregorianCalendar.YEAR)) ? 1 : 0;
return daysInMonths[c.get(GregorianCalendar.MONTH)];
}
Actually, the Calendar class provides a method to that very simply. For a given Calendar or GregorianCalendar object : calObject.getActualMaximum(calobject.DAY_OF_MONTH)
In the Java API documentation there is a note saying that The version (getActualMaximum()) of this function on Calendar uses an iterative algorithm to determine the actual maximum value for the field. There is almost always a more efficient way to accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar overrides this function with a more efficient implementation. So it looks like it's a lot more efficient to call getActualMaximum from a GregorianCalendar object than a Calendar object. (Thanks to P. Harris for the tip) gregCalObject.getActualMaximum(gregCalObject.DAY_OF_MONTH)
解决方案 »
- 为什么用setOnClickListener(this)报错:in the type View is not applicable for the argume
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 想请教下有没有人做过SAPJCO在solaris服务下的websphere联调的问题
- 菜鸟问题 新手上路 请帮忙 急!分数以后一定补上!
- 新人求解,这到底哪里出错了
- readLine()函数读不出东西,怎么回事啊
- jb7编写代码时,无法录入汉字
- 菜鸟求救!(non-static variable this cannot be referenced from a static context)
- 若干问题?
- 求教一个正则表示式,过滤非纯数字和非纯字母
- JDK安装过程中莫名中断,怎么解决
- *****************ORACLE超级难题********************************
- 希望得到大虾们的帮助:)
public int getDayOfCurrentMonth(int iYear, int iMonth) {
int iDayCount = 31; Calendar newCalendar = Calendar.getInstance();
newCalendar.set(iYear, iMonth+1, 1);
newCalendar.add(Calendar.DATE, -1);
int iDay = newCalendar.get(Calendar.DATE);
if(iDay>10) iDayCount = iDay;
return iDayCount;
}注意传过来的Month的值哟。 java中的5月,在函数里面的值其实是4。嘻嘻,我想的方法好不好?
那个if(iDay>10) iDayCount = iDay;不要。我当时是用来测试用的!
thisMonth.set(Calendar.MONTH, your月);
thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
thisMonth.set(Calendar.DAY_OF_MONTH,1);
int firstIndex=thisMonth.get(Calendar.DAY_OF_WEEK)-1;
int maxIndex=thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
maxIndex-firstIndex即是