获取当前天之前N天的数据,获取当前天前5天的月的最后一天

public static String getDateBefore(int day) {
   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
   String time;

   Date date = new Date();
   Calendar no = Calendar.getInstance();
   no.setTime(date);
   no.set(Calendar.DATE, no.get(Calendar.DATE) - day);
   time = sdf.format(no.getTime());

   return time;
}


public static List getLastdayMonth(String dateStr){
   SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
   Calendar calendar = Calendar.getInstance();
   List dayStrList = new ArrayList<>();
   try{
      Date date = df.parse(dateStr);
      calendar.setTime(date);
      // 上个月最后一天
      String day_last;
      for(int i=1;i<6;i++){
         calendar.set(Calendar.DATE, 1); // 设置为该月第一天
         calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天
         day_last = df.format(calendar.getTime());
         StringBuffer endStr = new StringBuffer().append(day_last);
         day_last = endStr.toString();
         dayStrList.add(day_last);
      }
   }catch (Exception e){
      log.error("ParseException:", e);
   }
   return dayStrList;

}

你可能感兴趣的:(获取当前天之前N天的数据,获取当前天前5天的月的最后一天)