1、根据传入时间获取该时间所在季度的第一天
/** * 根据传入时间获取传入日期所在季度的第一天的日期 * * @param startDate * @return */ public static String quarterStart(String startDate) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dBegin = null; try { dBegin = sdf.parse(startDate); } catch (ParseException e) { throw new RuntimeException(e); } Calendar calBegin = Calendar.getInstance(); calBegin.setTime(dBegin); int remainder = calBegin.get(Calendar.MONTH) % 3; int month = remainder != 0 ? calBegin.get(Calendar.MONTH) - remainder : calBegin.get(Calendar.MONTH); calBegin.set(Calendar.MONTH, month); calBegin.set(Calendar.DAY_OF_MONTH, calBegin.getActualMinimum(Calendar.DAY_OF_MONTH)); calBegin.setTime(calBegin.getTime()); return sdf.format(calBegin.getTime()); }
2、获取一段时间段内的每一天日期
/** * 获取时间段内的每一天[2023-08-02 ~ 2023-09-20] * * @param start * @param end * @return */ public static List<String> getBetweenDate(String start, String end) { List<String> list = new ArrayList<>(); LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); long distance = ChronoUnit.DAYS.between(startDate, endDate); if (distance < 1) { return list; } Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString())); return list; }
3、获取一段时间段内每个月的日期
/** * 获取时间段内的所有月份[年月] * * @param start * @param end * @return */ public static List<String> getBetweenMonth(String start, String end) { List<String> list = new ArrayList<>(); LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); long distance = ChronoUnit.MONTHS.between(startDate, endDate); Stream.iterate(startDate, d -> d.plusMonths(1)).limit(distance + 1).forEach(f -> { list.add(f.toString().substring(0, 7)); }); return list; }