获得指定月的最后一天和第一天

  获取指定年月的第一天

/**
     * 获取指定年月的第一天
     * @param year
     * @param month
     * @return
	 * @throws ParseException 
     */
    @SuppressWarnings("unused")
	private String getFirstDayOfMonth1(String date) throws ParseException { 
    	//格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date();
        
        if(StringUtils.isNotBlank(date)){
        	curDate = sdf.parse(date);
        }
        
        Calendar cal = Calendar.getInstance(); 
        
        cal.setTime(curDate);
        
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数 
        cal.set(Calendar.DAY_OF_MONTH,firstDay);  
        
        return df.format(cal.getTime());  
    }

 获取指定年月的最后一天

/**
     * 获取指定年月的最后一天
     * @param year
     * @param month
     * @return
     * @throws ParseException 
     */
     @SuppressWarnings("unused")
	private  String getLastDayOfMonth1(String date) throws ParseException {  
    	//格式化日期
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); 
         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
         Calendar cal = Calendar.getInstance();   
         
         Date curDate = new Date();
         
         if(StringUtils.isNotBlank(date)){
         	curDate = sdf.parse(date);
         }
         
         cal.setTime(curDate);
         //获取某月最大天数
         int lastDay = cal.getActualMaximum(Calendar.DATE);
         //设置日历中月份的最大天数  
         cal.set(Calendar.DAY_OF_MONTH, lastDay);  
          
         return df.format(cal.getTime());
     }

猜你喜欢

转载自blog.csdn.net/qq_31546841/article/details/83108365