java中获取两个时间段之间的集合案例

在java开发中或多或少的接触到对于时间的划分,那么我以我所用到的算出两个时间集合的方法。

一:获取两个时间段的所有日期集合

    public static List<String> getTimeOriginalList(String startDate, String endDate) {
        SimpleDateFormat sdf;
        int calendarType;

        switch (startDate.length()) {
            case 19:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                calendarType = Calendar.DATE;
                break;
            case 10:
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                calendarType = Calendar.DATE;
                break;
            case 7:
                sdf = new SimpleDateFormat("yyyy-MM");
                calendarType = Calendar.MONTH;
                break;
            case 4:
                sdf = new SimpleDateFormat("yyyy");
                calendarType = Calendar.YEAR;
                break;
            default:
                return null;
        }

        List<String> result = new ArrayList<>();
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        try {
            min.setTime(sdf.parse(startDate));
            min.add(calendarType, 0);
            max.setTime(sdf.parse(endDate));
            max.add(calendarType, 0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar curr = min;
        while (curr.before(max)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            result.add(formatter.format(min.getTime()));
            curr.add(calendarType, 1);
        }
        return result;
    }

二:判断当前日期是否在两个时间段的区间

    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

三:根据开始时间和结束时间以周划分

public static List<String[]> getType(Date sd, Date ed) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long days = (ed.getTime() - sd.getTime()) / 3600 / 24 / 1000;

        Calendar instance = Calendar.getInstance();
        instance.setTime(sd);
        int i = instance.get(Calendar.DAY_OF_WEEK);
        int fsi = 7 - i + 1;
        long iiv = days - fsi;
        //天数
        long d = iiv / 7;

        instance.add(Calendar.DAY_OF_YEAR, fsi);

        List<String[]> list = new ArrayList<>();
        list.add(new String[]{df.format(sd), df.format(instance.getTime())});

        String[] ne;
        for (long k = 0; k < d; k++) {
            instance.add(Calendar.DAY_OF_YEAR, 1);
            Date start = instance.getTime();
            instance.add(Calendar.DAY_OF_YEAR, 6);
            Date end = instance.getTime();
            ne = new String[]{df.format(start), df.format(end)};
            list.add(ne);
        }

        instance.add(Calendar.DAY_OF_YEAR, 1);
        list.add(new String[]{df.format(instance.getTime()), df.format(ed)});

        return list;

    }

四:根据两个时间段以月划分

    public static List<String[]> getMonthType(Date sd, Date ed) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long days = (ed.getTime() - sd.getTime()) / 3600 / 24 / 1000;

        Calendar instance = Calendar.getInstance();
        instance.setTime(sd);
        int i = instance.get(Calendar.DAY_OF_MONTH);
        int fsi = 30 - i + 1;
        long iiv = days - fsi;
        //天数
        long d = iiv / 30;

        instance.add(Calendar.DAY_OF_YEAR, fsi);

        List<String[]> list = new ArrayList<>();
        list.add(new String[]{df.format(sd), df.format(instance.getTime())});

        String[] ne;
        for (long k = 0; k < d; k++) {
            instance.add(Calendar.DAY_OF_YEAR, 1);
            Date start = instance.getTime();
            instance.add(Calendar.DAY_OF_YEAR, 29);
            Date end = instance.getTime();
            ne = new String[]{df.format(start), df.format(end)};
            list.add(ne);
        }

        instance.add(Calendar.DAY_OF_YEAR, 1);
        list.add(new String[]{df.format(instance.getTime()), df.format(ed)});

        return list;

    }

以上是我在工作中所用到的时间划分的一些模板,如果哪有不对的地方,欢迎大家指出。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值