java获取时间段内的自然周和满一个月的时间

    /**
     * 获取时间段内的自然周
     *
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<Set<String>> getNatureWeeks(String dateStart, String dateEnd) {
        List<Set<String>> natureWeekList = new ArrayList<>();
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            int startDateNum = Integer.parseInt(dateStart);
            int endDateNum = Integer.parseInt(dateEnd);
            if (startDateNum >= endDateNum) {
                return natureWeekList;
            }
            Date startDate = dateFormat.parse(dateStart);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(startDate);
            for (; ; ) {
                int dayOfWeeek = new Integer(calendar.get(Calendar.DAY_OF_WEEK));
                //周一(周日-周一...周五-周六依次用1-7表示)
                if (dayOfWeeek == 2) {
                    calendar.add(Calendar.DATE, 6);
                    int dateNum = Integer.parseInt(dateFormat.format(calendar.getTime()));
                    if (dateNum <= endDateNum) {
                        Set natureWeek = new LinkedHashSet();
                        calendar.add(Calendar.DATE, -6);
                        for (int i = 0; i < 7; i++) {
                            String day = dateFormat.format(calendar.getTime());
                            natureWeek.add(day);
                            calendar.add(Calendar.DATE, 1);
                        }
                        natureWeekList.add(natureWeek);
                    } else {
                        break;
                    }
                } else {
                    calendar.add(Calendar.DATE, 1);
                }
            }
        } catch (ParseException e) {
            System.out.print("获取时间段内的自然周异常,"+e.toString());
        }
        return natureWeekList;
    }



    /**
     * 获取时间段内的满月
     *
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<Set<String>> getFullMonth(String dateStart, String dateEnd) {
        List<Set<String>> fullMonthList = new ArrayList<>();
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            //起始时间
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(dateFormat.parse(dateStart));
            int startDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            //获取当前月的第一天
            int firstDayOfMonth = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
            if (firstDayOfMonth < startDayOfMonth) {
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, firstDayOfMonth);
            }
            //结束时间
            Calendar cld = new GregorianCalendar();
            cld.setTime(dateFormat.parse(dateEnd));
            int endDayOfMonth = cld.get(Calendar.DAY_OF_MONTH);
            //获取当月最后一天
            int lastDayOfyMonth = cld.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (endDayOfMonth < lastDayOfyMonth) {
                cld.add(Calendar.MONTH, -1);
                cld.set(Calendar.DAY_OF_MONTH, cld.getActualMaximum(Calendar.DAY_OF_MONTH));
            }
            int endDateNum = Integer.parseInt(dateFormat.format(cld.getTime()));
            for (; ; ) {
                int startDateNum = Integer.parseInt(dateFormat.format(calendar.getTime()));
                if (startDateNum < endDateNum) {
                    Set monthSet = new LinkedHashSet();
                    int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    for (int i = 0; i < maxDay; i++) {
                        monthSet.add(dateFormat.format(calendar.getTime()));
                        calendar.add(Calendar.DATE, 1);
                    }
                    fullMonthList.add(monthSet);
                } else {
                    break;
                }
            }

        } catch (Exception e) {
            System.out.print("获取时间段内的满月异常,"+e.toString());
        }

        return fullMonthList;
    }

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要按、月、天和年对给定的时间进行分组,可以使用Java中的日期时间库来处理。以下是一个示例代码,展示了如何按照不同的时间单位进行分组: ```java import java.time.LocalDate; import java.time.LocalDateTime; import java.time.YearMonth; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class GroupByTimeExample { public static void main(String[] args) { // 假设有一个起始日期和一个结束日期 LocalDate startDate = LocalDate.of(2022, 1, 1); LocalDate endDate = LocalDate.of(2022, 12, 31); // 创建四个Map来存储按不同时间单位分组的结果 Map<YearMonth, List<LocalDate>> groupedByMonth = new HashMap<>(); Map<LocalDate, List<LocalDate>> groupedByDay = new HashMap<>(); Map<Integer, List<LocalDate>> groupedByWeek = new HashMap<>(); Map<Integer, List<LocalDate>> groupedByYear = new HashMap<>(); // 遍历时间内的每一天,将每个日期按照不同的时间单位添加到对应的组中 LocalDate currentDate = startDate; while (!currentDate.isAfter(endDate)) { YearMonth yearMonth = YearMonth.from(currentDate); if (!groupedByMonth.containsKey(yearMonth)) { groupedByMonth.put(yearMonth, new ArrayList<>()); } groupedByMonth.get(yearMonth).add(currentDate); if (!groupedByDay.containsKey(currentDate)) { groupedByDay.put(currentDate, new ArrayList<>()); } groupedByDay.get(currentDate).add(currentDate); int week = currentDate.get(ChronoUnit.WEEK_OF_YEAR); if (!groupedByWeek.containsKey(week)) { groupedByWeek.put(week, new ArrayList<>()); } groupedByWeek.get(week).add(currentDate); int year = currentDate.getYear(); if (!groupedByYear.containsKey(year)) { groupedByYear.put(year, new ArrayList<>()); } groupedByYear.get(year).add(currentDate); currentDate = currentDate.plusDays(1); // 增加一天 } // 打印按不同时间单位分组的结果 System.out.println("Grouped by Month:"); for (Map.Entry<YearMonth, List<LocalDate>> entry : groupedByMonth.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } System.out.println("\nGrouped by Day:"); for (Map.Entry<LocalDate, List<LocalDate>> entry : groupedByDay.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } System.out.println("\nGrouped by Week:"); for (Map.Entry<Integer, List<LocalDate>> entry : groupedByWeek.entrySet()) { System.out.println("Week " + entry.getKey() + ": " + entry.getValue()); } System.out.println("\nGrouped by Year:"); for (Map.Entry<Integer, List<LocalDate>> entry : groupedByYear.entrySet()) { System.out.println("Year " + entry.getKey() + ": " + entry.getValue()); } } } ``` 在这个例子中,我们给定了一个起始日期和一个结束日期,并使用`LocalDate`、`YearMonth`、`Integer`等作为键来将日期按不同的时间单位分组。最终的结果是四个`Map`,分别表示按月、天、和年分组的日期列表。 运行以上代码,输出将会是: ``` Grouped by Month: 2022-01: [2022-01-01, 2022-01-02, ..., 2022-01-31] 2022-02: [2022-02-01, 2022-02-02, ..., 2022-02-28] ... 2022-12: [2022-12-01, 2022-12-02, ..., 2022-12-31] Grouped by Day: 2022-01-01: [2022-01-01] 2022-01-02: [2022-01-02] ... 2022-12-31: [2022-12-31] Grouped by Week: Week 1: [2022-01-01, 2022-01-02, ..., 2022-01-07] Week 2: [2022-01-08, 2022-01-09, ..., 2022-01-14] ... Week 52: [2022-12-24, 2022-12-25, ..., 2022-12-31] Grouped by Year: Year 2022: [2022-01-01, 2022-01-02, ..., 2022-12-31] ``` 这样,你就成功地按、月、天和年分组了。你可以根据自己的需求对起始日期、结束日期和输出格式进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值