如何在两者之间获取日历日期?

时间:2016-07-25 07:59:22

标签: java calendar

我想在日历之间获取日期,但由于某种原因,这不起作用。有什么建议吗?感谢。

public boolean verifyDate(Calendar cal) {       
    Calendar toDate  =  Calendar.getInstance();
    toDate.setTime(passToDate);
    Calendar fromDate  =  Calendar.getInstance();
    fromDate.setTime(passFromDate);
    return !((fromDate.after(cal) && toDate.before(cal)) || DateUtils.isSameDay(fromDate, cal) || DateUtils.isSameDay(toDate, cal));
}

2 个答案:

答案 0 :(得分:0)

fromDate.after(cal) - 如果true位于fromDate之后,将返回cal,与before类似。

如果您想检查cal是否在两个日期之间,请使用:

return cal.after(fromDate) && cal.before(toDate) 
    && !(DateUtils.isSameDay(fromDate, cal) || DateUtils.isSameDay(toDate, cal));

答案 1 :(得分:0)

public static List<String> getDates(String startDate, String endDate)
            throws ParseException, java.text.ParseException {
        List<String> dateList = new ArrayList<String>();
        if (startDate == null || startDate.equals("") || endDate == null || endDate.equals("")) {
            return null;
        }


        Calendar calendar = new GregorianCalendar();


        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = dateFormat.parse(startDate);
        Date d2 = dateFormat.parse(endDate);

        Calendar calendar2 = new GregorianCalendar();
        calendar2.setTime(d2);
        calendar2.add(Calendar.DATE, 1);
        calendar.setTime(d1);

        while (calendar.getTime().before(calendar2.getTime()))
        {
            Date result = calendar.getTime();
            dateList.add(dateFormat.format(result));
            calendar.add(Calendar.DATE, 1);
        }


        return dateList;
    }