有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中检查日期是否介于两个日期之间

我想知道的一件事是如何计算从今天起10天的日期

第二件事是检查一个日期是否在另外两个日期之间。 例如,假设我有一个应用程序,它显示了我在未来10天内需要做的事情(planner)。现在,我如何查看我分配给某个事件的日期是否介于今天和距离今天10天的日期之间


共 (6) 个答案

  1. # 2 楼答案

    public static boolean between(Date date, Date dateStart, Date dateEnd) {
        if (date != null && dateStart != null && dateEnd != null) {
            if (date.after(dateStart) && date.before(dateEnd)) {
                return true;
            }
            else {
                return false;
            }
        }
        return false;
    }
    

    编辑:另一个建议的变体:

    public Boolean checkDate(Date startDate, Date endDate, Date checkDate) { 
        Interval interval = new Interval(new DateTime(startDate), 
                                         new DateTime(endDate));   
        return interval.contains(new DateTime(checkDate)); 
    }
    
  2. # 3 楼答案

    使用java.util.Datejava.util.Calendar操纵和比较日期是一件非常痛苦的事情,这就是JodaTime存在的原因。到目前为止,没有一个答案涵盖了所讨论的时间。当日期的时间不为零时,比较可能会失败。还不清楚您想要的是包容性还是排他性比较。到目前为止发布的大多数答案都建议进行独家比较(即5月24日是而不是在5月20日和5月24日之间),而在实际情况下,将其包括(即5月24日在5月20日和5月24日之间)


    One thing I want to know is how to calculate what date will it be 10 days from today.

    使用标准的JavaSE6API,您需要^{}来实现这一点

    Calendar plus10days = Calendar.getInstance();
    plus10days.add(Calendar.DAY_OF_YEAR, 10);
    

    有了JodaTime,您可以这样做:

    DateTime plus10days = new DateTime().plusDays(10);
    

    Second thing is to check if one Date is between two other Dates. For example, let's say I have an app that shows what events I need to do in the next 10 days (planner). Now how can I see if the date I assigned to an event is between today and the date that is 10 days from today?

    现在是Calendar的可怕部分。让我们先准备:

    Calendar now = Calendar.getInstance();
    Calendar plus10days = Calendar.getInstance();
    plus10days.add(Calendar.DAY_OF_YEAR, 10);
    Calendar event = Calendar.getInstance();
    event.set(year, month - 1, day); // Or setTime(date);
    

    为了使用Calendar#before()Calendar#after()进行可靠的比较,我们需要先去掉时间。想象一下,现在是2010年5月24日上午9点,活动日期被设定为2010年5月24日,没有时间。当你想要包容性比较时,你希望在同一天进行return true。即(event.equals(now) || event.after(now))或-更短但相等的(!event.before(now))都应返回true。但实际上,由于now中存在时间,没有人会这样做。首先需要清除所有日历实例中的时间,如下所示:

    calendar.clear(Calendar.HOUR);
    calendar.clear(Calendar.HOUR_OF_DAY);
    calendar.clear(Calendar.MINUTE);
    calendar.clear(Calendar.SECOND);
    calendar.clear(Calendar.MILLISECOND);
    

    或者,您也可以仅在日/月/年进行比较

    if (event.get(Calendar.YEAR) >= now.get(Calendar.YEAR)
        && event.get(Calendar.MONTH) >= now.get(Calendar.MONTH)
        && event.get(Calendar.DAY_OF_MONTH) >= now.get(Calendar.DAY_OF_MONTH)
    {
        // event is equal or after today.
    }
    

    非常冗长

    使用JodaTime,只需使用^{}即可获得日期部分:

    LocalDate now = new DateTime().toLocalDate();
    LocalDate plus10days = now.plusDays(10);
    LocalDate event = new DateTime(year, month, day, 0, 0, 0, 0).toLocalDate();
    if (!event.isBefore(now) && !event.isAfter(plus10days)) {
        // Event is between now and 10 days (inclusive).
    }
    

    是的,以上是你真正需要做的

  3. # 5 楼答案

    要检查日期是否介于两个日期之间,下面是一个简单的程序:

    public static void main(String[] args) throws ParseException {
    
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    
        String oeStartDateStr = "04/01/";
        String oeEndDateStr = "11/14/";
    
        Calendar cal = Calendar.getInstance();
        Integer year = cal.get(Calendar.YEAR);
    
        oeStartDateStr = oeStartDateStr.concat(year.toString());
        oeEndDateStr = oeEndDateStr.concat(year.toString());
    
        Date startDate = sdf.parse(oeStartDateStr);
        Date endDate = sdf.parse(oeEndDateStr);
        Date d = new Date();
        String currDt = sdf.format(d);
    
    
        if((d.after(startDate) && (d.before(endDate))) || (currDt.equals(sdf.format(startDate)) ||currDt.equals(sdf.format(endDate)))){
            System.out.println("Date is between 1st april to 14th nov...");
        }
        else{
            System.out.println("Date is not between 1st april to 14th nov...");
        }
    }
    
  4. # 6 楼答案

    加上十天:

    Date today = new Date();
    Calendar cal = new GregorianCalendar();
    cal.setTime(today);
    cal.add(Calendar.DAY_OF_YEAR, 10);
    

    要检查两个日期之间是否存在差异:

    myDate.after(firstDate) && myDate.before(lastDate);