This is a really simple request, but I am not quite sure the easiest/most efficient way of generating these two values.
I need to write a script that will check whether a given value is between two values. I am well aware of how this is done in SQL.
The way I need the values is 开发者_开发技巧somethign similar to the following.
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
Again, the Java Date() type may not be the best practice to do something like this. In the end I just need to generate three values that I can say
if testValue is after beginningOfDay && testValue is before endOfDay
//do logic
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
}
You can use a calendar object to do this and by the way, the way you did the bounds check in your question is wrong (your date can match the before/after dates and still be considered in range). The following shows whether a date falls on a certain day of the year. It assumes that the timezones for the dateTime to check and the day are equal and that no time adjustments took place:
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...
}
This can be further simplified to:
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
}
Here's something I've used in my own code to determine if a file was modified on a certain day. Like the other answers in this thread, I used the Calendar.
// 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
}
This might be closer to what you are looking for since you only need to see if the event falls on a certain day.
精彩评论