这是一个非常简单的请求,但我不太确定生成这两个值的最简单/最有效的方法.
我需要编写一个脚本来检查给定值是否在两个值之间.我很清楚这是如何在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
Run Code Online (Sandbox Code Playgroud)
同样,Java Date()类型可能不是执行此类操作的最佳实践.最后,我只需要生成三个可以说的值
if testValue is after beginningOfDay && testValue is before endOfDay
//do logic
Run Code Online (Sandbox Code Playgroud)
Personally I use the Calendar object for this. For example:
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
}
Run Code Online (Sandbox Code Playgroud)