java判断某年某月有几天,可以这样弄:// 将日历里的日设为0,日历就会倒转到上一个月的最后一天

 

 1 public static int getDaysOfYearMonth(int year, int month){
 2 
 3 Calendar cal = Calendar.getInstance();
 4 
 5 cal.set(Calendar.YEAR, year);
 6 
 7 cal.set(Calendar.MONTH, month - 1);
 8 cal.set(Calendar.DAY_OF_MONTH, 1);
9 cal.add(Calendar.MONTH, 1); 10 // 将日历里的日设为0,日历就会倒转到上一个月的最后一天 11 12 cal.set(Calendar.DAY_OF_MONTH, 0); 13
  return cal.get(Calendar.DAY_OF_MONTH); 14 }

 

 

 

  /**
     * 获取某年某月最后一天
     * 此函数有缺陷: windows单元测试没有问题\linux单独运行此函数没有问题\linux联调会跳出这样的结果:2019年2月最后一天是31天
     * 
     * @param year 年
     * @param monthFrom1 1-12月
     * @author 
     * @return
     */
    public static int getDaysOfMonth1(int year, int monthFrom1) {
        logger.info("Year=" + year + ", monthFrom1=" + monthFrom1);

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthFrom1 - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        {
            for (int m = 1; m <= 13; m++) {
                cal.set(Calendar.MONTH, m - 1);
                int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                logger.info("Year=" + year + ", m=" + m + ", days=" + days);
            }
        }

        logger.info("Year=" + year + ", monthFrom1=" + monthFrom1 + ", lastDay=" + lastDay);
        return lastDay;
    }

 

Java获得某月第一天和最后一天(Calendar实现)

 

  1. import java.text.SimpleDateFormat;
  2.  
    import java.util.Calendar;
  3.  
    /**
  4.  
    * 获得该月第一天
  5.  
    * @param year
  6.  
    * @param month
  7.  
    * @return
  8.  
    */
  9.  
    publicstatic String getFirstDayOfMonth(int year,int month){
  10.  
    Calendar cal = Calendar.getInstance();
  11.  
    //设置年份
  12.  
    cal.set(Calendar.YEAR,year);
  13.  
    //设置月份
  14.  
    cal.set(Calendar.MONTH, month-1);
  15.  
    //获取某月最小天数
  16.  
    int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
  17.  
    //设置日历中月份的最小天数
  18.  
    cal.set(Calendar.DAY_OF_MONTH, firstDay);
  19.  
    //格式化日期
  20.  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  21.  
    String firstDayOfMonth = sdf.format(cal.getTime());
  22.  
    return firstDayOfMonth;
  23.  
    }
  24.  
     
  25.  
    /**
  26.  
    * 获得该月最后一天
  27.  
    * @param year
  28.  
    * @param month
  29.  
    * @return
  30.  
    */
  31.  
    publicstatic String getLastDayOfMonth(int year,int month){
  32.  
    Calendar cal = Calendar.getInstance();
  33.  
    //设置年份
  34.  
    cal.set(Calendar.YEAR,year);
  35.  
    //设置月份
  36.  
    cal.set(Calendar.MONTH, month-1);
  37.  
    //获取某月最大天数
  38.  
    int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  39.  
    //设置日历中月份的最大天数
  40.  
    cal.set(Calendar.DAY_OF_MONTH, lastDay);
  41.  
    //格式化日期
  42.  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  43.  
    String lastDayOfMonth = sdf.format(cal.getTime());
  44.  
    return lastDayOfMonth;
  45.  
    }
  46.