String searchbound = "2006-06-16";
我要得到30String searchbound = "2006-05-16";
我要得到31String searchbound = "2006-02-01";
我要得到28
就是说,将这个字符型数据的参数接过来以后,我要把他转换成日期型后,怎么计算这天的月份最后一天是多少?

解决方案 »

  1.   

    /**
         * 根据传入的时间字符串得到该时间月份的天数
         * @param datestr String 时间字符串
         * @param format String 时间字符串格式
         * @return int 当月天数
         * @throws ParseException
         */
        public static int getRemainDayInMonth(String datestr, String format)
            throws ParseException
        {
            DateFormat df = new SimpleDateFormat(format) ;
            Calendar c = Calendar.getInstance();
            c.setTime(df.parse(datestr)); 
            
            int month = c.get(Calendar.MONTH) ; //current month
            
            c.set(Calendar.MONTH, month+1); //next month
            c.set(Calendar.DAY_OF_MONTH, 1);//first day
            c.add(Calendar.DAY_OF_MONTH, -1) ; // date minus one day
            
            return c.get(Calendar.DAY_OF_MONTH) ; //get day of month
        }