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;

    }

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

你可能感兴趣的:(java,时间,java)