求某年某月有多少天

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

Calendar calendar = Calendar.getInstance();

calendar.set(Integer.valueOf(2015),Integer.valueOf(2)-1 ,1); //月份从0开始,所以2代表的是3月份,要-1

System.out.println(sdf.format(calendar.getTime())); //2015-02-01

calendar.roll(Calendar.DATE, -1);  

System.out.println(sdf.format(calendar.getTime()));  //2015-02-28

int days = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(days); //28

posted @ 2015-03-03 16:34  庄姜琴瑟  阅读(175)  评论(0编辑  收藏  举报