请...问... 有给出年月 获得指定月份的天数, 有库函数吗?

jojo015 2006-03-09 01:13:57
比如我给出 1999年1月 返回31天
...全文
190 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dan1980 2006-03-09
  • 打赏
  • 举报
回复
楼上几位的代码是不是有点小题大做?每次得到月份天数都要创建一个Calendar对象?有必要吗?
wizardblue 2006-03-09
  • 打赏
  • 举报
回复
public static int getDays(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);// the month between 0-11
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}
做鸡真好吃 2006-03-09
  • 打赏
  • 举报
回复
Good~
wizardblue 2006-03-09
  • 打赏
  • 举报
回复
import java.util.Calendar;
import java.util.Date;

public class DateDemo {
public static void main(String[] args) {
System.out.println(getDays(2004,2));
System.out.println(getDays(1999,1));
}
public static int getDays(int year,int month){
Date d= new Date(year-1900,month-1,1);
Calendar c = Calendar.getInstance();
c.setTime(d);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
diggywang 2006-03-09
  • 打赏
  • 举报
回复
唉,何苦呢?库也是这样编出来的........
jojo015 2006-03-09
  • 打赏
  • 举报
回复
我知道不难, 但如果有库方法的话何乐不为呢?
Dan1980 2006-03-09
  • 打赏
  • 举报
回复
不好意思,上面写错了,switch(year)应该是switch(month),呵呵
Dan1980 2006-03-09
  • 打赏
  • 举报
回复
这么简单的,自己写一个不就行了?

package myutilites;

public class Calendar {
public static int getMonthDays(int year, int month) {
switch(year) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28);
}
}
}

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧