项目中经常需要处理日期,下面就常用日期处理方法进行总结统计:
1、判断是否是闰年
1 public boolean isLeapYear(int year) { 2 return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)); 3 }
2、根据年和月份计算这个月的最后一天
1 protected int getLastDayOfMonth(int monthNum, int year) { 2 3 switch (monthNum) { 4 case 1: 5 return 31; 6 case 2: 7 return (isLeapYear(year)) ? 29 : 28; 8 case 3: 9 return 31; 10 case 4: 11 return 30; 12 case 5: 13 return 31; 14 case 6: 15 return 30; 16 case 7: 17 return 31; 18 case 8: 19 return 31; 20 case 9: 21 return 30; 22 case 10: 23 return 31; 24 case 11: 25 return 30; 26 case 12: 27 return 31; 28 default: 29 throw new IllegalArgumentException("Illegal month number: " 30 + monthNum); 31 } 32 }
时间: 2024-10-20 07:50:49