获取最近连续日期年月日

近一个月年月日获取

 // i 取 7,15 ,30 即可获取仅一周,半月,一个月的 时间集合 
 List<String>  recentTimeLIst  =  ArrayList<String>();
  for (int i = 0; i <30 ; i++) {
            Calendar test = Calendar.getInstance();
            SimpleDateFormat dd  = new SimpleDateFormat("yyyy-MM-dd");
            test.add(Calendar.DATE, -i);
            String formatTime= dd.format(test.getTime());
            recentTimeLIst.add(formatTime);
        }

近几个月第一天和最后一天的集合获取

// 最近几个月最后一天的获取
public List<String> getResultMonthLastDay(int month) {
        List<String> resultList = new ArrayList<String>();
        for (int i = 0; i < month; i++) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, -i);
            c.set(Calendar.DAY_OF_MONTH,c.getActualMaximum(Calendar.DAY_OF_MONTH));
            String monthLastDay = dd.format(c.getTime());
            resultList.add(monthLastDay);
        }
        return resultList;
    }

// 最近几个月第一天的获取
 public List<String> getResultMonthFirstDay(int month) {
        List<String> resultList = new ArrayList<String>();
        for (int i = 0; i < month; i++) {
            String localDate = LocalDate.now().minusMonths(i).toString();
            String s = localDate.substring(0, localDate.lastIndexOf("-")) + "-01";
            resultList.add(s);
        }
        return resultList;
    }

当月每一天的获取

猜你喜欢

转载自blog.csdn.net/WindwirdBird/article/details/105781906