java-以月为单位,得到一年中某一个月份的范围

简单说:得到某一月份的第一天,第一小时,第一分钟,第一秒和某一月份的最后一天,最后一小时,最后一分钟,最后一秒。

实现:

 1 //月初
 2 private Date getBeginTimeOfMonth(int year , int month){
 3         Calendar c = Calendar.getInstance();
 4         c.set(Calendar.YEAR,year);
 5         c.set(Calendar.MONTH,month-1);
 6         c.set(Calendar.DAY_OF_MONTH,1);
 7         c.set(Calendar.HOUR_OF_DAY,0);
 8         c.set(Calendar.MINUTE,0);
 9         c.set(Calendar.SECOND,0);
10         c.set(Calendar.MILLISECOND,0);
11         return c.getTime();
12     }
13 //月末
14 private Date getEndTimeOfMonth(int year , int month){
15         Calendar c = Calendar.getInstance();
16         c.set(Calendar.YEAR,year);
17         c.set(Calendar.MONTH,month-1);
18         int lastDate = c.getActualMaximum(Calendar.DATE);
19         c.set(Calendar.DAY_OF_MONTH,lastDate);
20         c.set(Calendar.HOUR_OF_DAY,23);
21         c.set(Calendar.MINUTE,59);
22         c.set(Calendar.SECOND,59);
23         c.set(Calendar.MILLISECOND,999);
24         return c.getTime();
25     }

 

  

posted @ 2015-02-04 14:38  哎呦喂,我的小祖宗╰つ  阅读(521)  评论(1编辑  收藏  举报