代码之家  ›  专栏  ›  技术社区  ›  TheJediCowboy

Java任务需要两个Date()值

  •  0
  • TheJediCowboy  · 技术社区  · 15 年前

    这是一个非常简单的请求,但是我不确定生成这两个值的最简单/最有效的方法。

    我需要编写一个脚本来检查给定值是否在两个值之间。我很清楚SQL是如何做到这一点的。

    Date testValue = new Date()       //This represents the value we are testing
    
    Date beginningOfDay = ....        //This value would represent the date for 
                                        testValue  at 12:00am
    
    Date endOfDay = ...               //This value would represent the date for 
                                       testValue at 11:59:59pm
    

    同样,Java Date()类型可能不是这样做的最佳实践。最后我只需要生成三个值

    if testValue is after beginningOfDay && testValue is before endOfDay 
         //do logic
    
    3 回复  |  直到 15 年前
        1
  •  5
  •   Mike Clark    15 年前

    就我个人而言,我使用Calendar对象。例如:

    Date testDate = ??? //Replace with whatever source you are using
    Calendar testDateCalendar = Calendar.getInstance();
    testDateCalendar.setTime(testDate);
    
    Date today = new Date();
    Calendar endOfDay = Calendar.getInstance();  //Initiates to current time
    endOfDay.setTime(today);
    endOfDay.set(Calendar.HOUR_OF_DAY, 23);
    endOfDay.set(Calendar.MINUTE, 59);
    endOfDay.set(Calendar.SECOND, 59);
    
    Calendar startOfDay = Calendar.getInstance();
    startOfDay.setTime(today);
    startOfDay.set(Calendar.HOUR_OF_DAY, 0);
    startOfDay.set(Calendar.MINUTE, 0);
    startOfDay.set(Calendar.SECOND, 0);
    
    if (startOfDay.before(testDateCalendar) && endOfDay.after(testDateCalendar))
    {
    //Whatever
    } else {
    //Failure
    }
    
        2
  •  2
  •   Michael Goldshteyn    15 年前

    Date dateTime=...
    Date day=...
    
    // This is the date we're going to do a range check on
    Calendar calDateTime=Calendar.getInstance();
    
    calDateTime.setTime(dateTime);
    
    // This is the day from which we will get the month/day/year to which 
    // we will compare it
    Calendar calDay=Calendar.getInstance();
    
    calDay.setTime(day);
    
    // Calculate the start of day time
    Calendar beginningOfDay=Calendar.getInstance();
    
    beginningOfDay.set(calDay.Get(Calendar.YEAR),
                       calDay.Get(Calendar.MONTH),
                       calDay.Get(Calendar.DAY_OF_MONTH),
                       0,  // hours
                       0,  // minutes
                       0); // seconds
    
    // Calculate the end of day time
    Calendar endOfDay=Calendar.getInstance();
    
    endOfDay.set(calDay.Get(Calendar.YEAR),
                 calDay.Get(Calendar.MONTH),
                 calDay.Get(Calendar.DAY_OF_MONTH),
                 23,  // hours
                 59,  // minutes
                 59); // seconds
    
    // Now, to test your date.
    // Note: You forgot about the possibility of your test date matching either 
    //       the beginning of the day or the end of the day. The accepted answer
    //       got this range check wrong, as well.
    if ((beginningOfDay.before(calDateTime) && endOfDay.after(calDateTime)) ||
         beginningOfDay.equals(calDateTime) || endOfDay.equals(calDateTime))
    {
      // Date is in range...
    }
    

    这可以进一步简化为:

    Date dateTime=...
    Date day=...
    
    // This is the date we're going to do a range check on
    Calendar calDateTime=Calendar.getInstance();
    
    calDateTime.setTime(dateTime);
    
    // This is the day from which we will get the month/day/year to which 
    // we will compare it
    Calendar calDay=Calendar.getInstance();
    
    calDay.setTime(day);
    
    if (calDateTime.get(YEAR)==calDay.get(YEAR) &&
        calDateTime.get(MONTH)==calDay.get(MONTH) &&
        calDateTime.get(DAY_OF_YEAR)==calDay.get(DAY_OF_YEAR))
    {
       // Date is in range
    }
    
        3
  •  0
  •   Charles Caldwell    15 年前

    以下是我在自己的代码中用来确定某一天是否修改了某个文件的内容。就像这篇文章中的其他答案一样,我使用了日历。

    // Get modified date of the current file
    // and today's date
    Calendar today = Calendar.getInstance();
    Calendar modDate = Calendar.getInstance();
    Date date = new Date(file.lastModified());
    modDate.setTime(date);
    // Convert dates to integers
    int modDay = modDate.get(Calendar.DAY_OF_YEAR);
    int todayDay = today.get(Calendar.DAY_OF_YEAR);
    if (modDay == todayDay) {
        // Do stuff
    }
    

    这可能更接近你想要的,因为你只需要看看事件是否发生在某一天。