java 根据时间戳区间动态返回前端标签

 


    /**
     *
     * 获取 年-月-日
     *      年/周
     *      年-月
     *      年
     * @param type  1  天  2 周  3 月  4 年
     * @param dateStart  开始时间戳
     * @param dateEnd  结束时间戳
     * */
    public static List<String> getTimeGroupString(Integer type, Long dateStart, Long dateEnd) {
        List<String> dateList = new ArrayList<String>();
        //逐日打印日期
        LocalDate startDate = Instant.ofEpochMilli(dateStart).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        LocalDate endDate = Instant.ofEpochMilli(dateEnd).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        /*
         几种方法用来判断日期,isBefore()、isAfter()、isEqual()
        */
        if (type ==1 || type ==2 || type == 3 || type == 4){
            getDateString(type, dateList, startDate);
            while (startDate.isBefore(endDate)) {
                if (type == 1) {
                    startDate = startDate.plusDays(1);
                }
                if (type == 2) {
                    startDate = startDate.plusDays(1);
                }
                if (type == 3) {
                    startDate = startDate.plusMonths(1);
                }
                if (type == 4) {
                    startDate = startDate.plusYears(1);
                }
                getDateString(type, dateList, startDate);
            }
        }
        return dateList;
    }

    private static void getDateString(Integer type, List<String> dateList, LocalDate startDate) {
        //天
        if (type == 1) {
            dateList.add(startDate.toString());
        }
        //年周
        if (type == 2) {
//        第一个参数:一周的第一天,不能为空
//        第二个参数:第一周的最小天数,从1到7
            WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
            int weekOfYear = startDate.get(weekFields.weekOfYear());
            String data = startDate.getYear() + "/" + weekOfYear;
            if (!dateList.contains(data)){
                dateList.add(data);
            }
        }
        //年月
        if (type == 3) {
            String data = startDate.getYear() + "-" + startDate.getMonthValue();
            dateList.add(data);
        }
        //年
        if (type == 4) {
            dateList.add(String.valueOf(startDate.getYear()));
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值