两个时间点间的日期集合

 两个时间点间的日期集合

private static List<String> getBetweenDates(String begin, String end) {
        try {
            List<String> result = new ArrayList<String>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Calendar tempStart = Calendar.getInstance();
            Date beginDate = sdf.parse(begin);
            Date endDate = sdf.parse(end);
            tempStart.setTime(beginDate);
            while (beginDate.getTime() <= endDate.getTime()) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
                beginDate = tempStart.getTime();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

两个时间点间的月份集合

public static List<Integer> getMonthBetween(String minDate, String maxDate) {
        try {
            ArrayList<Integer> result = new ArrayList<Integer>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
//            min.setTime(minDate);
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
//            max.setTime(maxDate);
            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {
                result.add(Integer.valueOf(sdf.format(curr.getTime())));
                curr.add(Calendar.MONTH, 1);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

获取当月所有日期的集合

public static List<String> getDateOfTheMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month) {
            list.add(sdf.format(cal.getTime()));
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }

 

上一篇:java---Calendar与Date


下一篇:c++ 引用的一个错