java得到当月天数

 

1.使用calendar类实现

  1. /** 
  2.  * 取得当月天数 
  3.  * */  
  4. public static int getCurrentMonthLastDay()  
  5. {  
  6.     Calendar a = Calendar.getInstance();  
  7.     a.set(Calendar.DATE, 1);//把日期设置为当月第一天   
  8.     a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天   
  9.     int maxDate = a.get(Calendar.DATE);  
  10.     return maxDate;  
  11. }  
  12.   
  13. /** 
  14.  * 得到指定月的天数 
  15.  * */  
  16. public static int getMonthLastDay(int year, int month)  
  17. {  
  18.     Calendar a = Calendar.getInstance();  
  19.     a.set(Calendar.YEAR, year);  
  20.     a.set(Calendar.MONTH, month - 1);  
  21.     a.set(Calendar.DATE, 1);//把日期设置为当月第一天   
  22.     a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天   
  23.     int maxDate = a.get(Calendar.DATE);  
  24.     return maxDate;  
  25. }  

 

2.使用自己编写的函数实现

 

  1. package test;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Calendar;  
  5. import java.util.Date;  
  6.   
  7. /** 
  8.  * 日期工具类 by hpf  
  9.  * */  
  10. public class DateUtils  
  11. {  
  12.     //返回当前年月日   
  13.     String getNowDate()  
  14.     {  
  15.         Date date = new Date();  
  16.         String nowDate = new SimpleDateFormat("yyyy年MM月dd日").format(date);  
  17.         return nowDate;  
  18.     }  
  19.   
  20.     //返回当前年份   
  21.     int getYear()  
  22.     {  
  23.         Date date = new Date();  
  24.         String year = new SimpleDateFormat("yyyy").format(date);  
  25.         return Integer.parseInt(year);  
  26.     }  
  27.   
  28.     //返回当前月份   
  29.     int getMonth()  
  30.     {  
  31.         Date date = new Date();  
  32.         String month = new SimpleDateFormat("MM").format(date);  
  33.         return Integer.parseInt(month);  
  34.     }  
  35.   
  36.     //判断闰年   
  37.     boolean isLeap(int year)  
  38.     {  
  39.         if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0))  
  40.             return true;  
  41.         else  
  42.             return false;  
  43.     }  
  44.   
  45.     //返回当月天数   
  46.     int getDays(int year, int month)  
  47.     {  
  48.         int days;  
  49.         int FebDay = 28;  
  50.         if (isLeap(year))  
  51.             FebDay = 29;  
  52.         switch (month)  
  53.         {  
  54.             case 1:  
  55.             case 3:  
  56.             case 5:  
  57.             case 7:  
  58.             case 8:  
  59.             case 10:  
  60.             case 12:  
  61.                 days = 31;  
  62.                 break;  
  63.             case 4:  
  64.             case 6:  
  65.             case 9:  
  66.             case 11:  
  67.                 days = 30;  
  68.                 break;  
  69.             case 2:  
  70.                 days = FebDay;  
  71.                 break;  
  72.             default:  
  73.                 days = 0;  
  74.                 break;  
  75.         }  
  76.         return days;  
  77.     }  
  78.   
  79.     //返回当月星期天数   
  80.     int getSundays(int year, int month)  
  81.     {  
  82.         int sundays = 0;  
  83.         SimpleDateFormat sdf = new SimpleDateFormat("EEEE");  
  84.         Calendar setDate = Calendar.getInstance();  
  85.         //从第一天开始   
  86.         int day;  
  87.         for (day = 1; day <= getDays(year, month); day++)  
  88.         {  
  89.             setDate.set(Calendar.DATE, day);  
  90.             String str = sdf.format(setDate.getTime());  
  91.             if (str.equals("星期日"))  
  92.             {  
  93.                 sundays++;  
  94.             }  
  95.         }  
  96.         return sundays;  
  97.     }  
  98.   
  99.     public static void main(String[] args)  
  100.     {  
  101.         DateUtils du = new DateUtils();  
  102.         System.out.println("今天日期是:" + du.getNowDate());  
  103.         System.out.println("本月有" + du.getDays(du.getYear(), du.getMonth()) + "天");  
  104.         System.out.println("本月有" + du.getSundays(du.getYear(), du.getMonth()) + "个星期天");  
  105.     }  
  106. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值