Java获取某年某月的第一天和最后一天

/**
	   * 获取某年某月的第一天
	   * @Title:getFisrtDayOfMonth
	   * @Description:
	   * @param:@param year
	   * @param:@param month
	   * @param:@return
	   * @return:String
	   * @throws
	   */
	  public static String getFisrtDayOfMonth(int year,int month){
	    Calendar cal = Calendar.getInstance();
	    //设置年份
	    cal.set(Calendar.YEAR,year);
	    //设置月份
	    cal.set(Calendar.MONTH, month-1);
	    //获取某月最小天数
	    int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
	    //设置日历中月份的最小天数
	    cal.set(Calendar.DAY_OF_MONTH, firstDay);
	    //格式化日期
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String firstDayOfMonth = sdf.format(cal.getTime());
	    return firstDayOfMonth;
	  }
	  /**
	   * 获取某月的最后一天
	   * @Title:getLastDayOfMonth
	   * @Description:
	   * @param:@param year
	   * @param:@param month
	   * @param:@return
	   * @return:String
	   * @throws
	   */
	  public static String getLastDayOfMonth(int year,int month)
	  {
	    Calendar cal = Calendar.getInstance();
	    //设置年份
	    cal.set(Calendar.YEAR,year);
	    //设置月份
	    cal.set(Calendar.MONTH, month-1);
	    //获取某月最大天数
	    int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	    //设置日历中月份的最大天数
	    cal.set(Calendar.DAY_OF_MONTH, lastDay);
	    //格式化日期
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String lastDayOfMonth = sdf.format(cal.getTime());
	    return lastDayOfMonth;
	  }

  

posted @ 2020-12-14 11:23  红尘沙漏  阅读(877)  评论(0编辑  收藏  举报