怎樣算出某年某月的最后一天?

解决方案 »

  1.   

    搂住:
     /**
         * Desc:TODO:
         * ljh
         * 2005-10-30
         * @param seeddate
         * @return
         * @throws Exception
         */
        public static String getLastDayOfMonthOfYear(String seeddate) throws Exception
        {
            //这里定义对seeddate的格式化
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String ret="";
            try {
                formatter.format(formatter.parse(seeddate));
                Calendar tempcl=formatter.getCalendar();
                //获得制定月份的最大天数
                int MaxDayOfMonthOfYear=tempcl.getActualMaximum(Calendar.DATE);
                //年月日重新组合
                ret=tempcl.get(Calendar.YEAR)+"-"+
                 (tempcl.get(Calendar.MONTH)+1)+"-"+MaxDayOfMonthOfYear;
                //对重新组合的年月日进行yyyy-MM-dd格式化
                ret=formatter.format(formatter.parseObject(ret));
                return ret;
            } catch (Exception ex) {
                //........其他错误处理............
                throw ex;
            }
        }
      

  2.   

    使用方法:
    try{
                String date="2005-7-1";
                System.out.println(getLastDayOfMonthOfYear(date));
                //得2005-07-31;
                
                date="2001-2-1";
                System.out.println(getLastDayOfMonthOfYear(date));
                //得2001-02-28;
            }
            catch(Exception ex)
            {
                
            }