Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java任务需要两个Date()值_Java_Date - Fatal编程技术网

Java任务需要两个Date()值

Java任务需要两个Date()值,java,date,Java,Date,这是一个非常简单的请求,但我不确定生成这两个值的最简单/最有效的方法 我需要编写一个脚本来检查给定值是否介于两个值之间。我很清楚这是如何在SQL中完成的 我需要这些值的方式与下面的类似 Date testValue = new Date() //This represents the value we are testing Date beginningOfDay = .... //This value would represent the date for

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

我需要编写一个脚本来检查给定值是否介于两个值之间。我很清楚这是如何在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

就我个人而言,我使用日历对象进行此操作。例如:

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
}

您可以使用日历对象来执行此操作,顺便说一句,您在问题中执行边界检查的方式是错误的(您的日期可以与之前/之后的日期匹配,并且仍然在范围内)。下面显示日期是否落在一年中的某一天。它假定要检查的日期时间和日期的时区相等,并且没有进行时间调整:

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
}

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

// 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
}

这可能更接近您所寻找的,因为您只需要查看事件是否在某一天发生

这有用吗?虽然我欣赏Java中的Calendar类,但我讨厌它造成的混乱。如果两个日历之间的日期发生变化(即午夜),则该类将失败。getInstance的将始终导致失败:
Calendar.before
Calendar.before
如果参数不是Calendar的实例,则始终返回
false
。Doc:“…当且仅当<代码>是日历实例时,否则,该方法返回false。”现在完美(如果我们忽略Java可以有更好的API)