How can I check times to ensure they are in sequential order?
I've got a JSP page which allows users to enter time periods throughout a 24 hour period, they are stored as Strings from the request such as :
- 13:00
- 14:00
- 15:00
Whilst iterating through these values, I need to perform a check to make sure that the time in question is after the previous one, such as the above.
I'm trying to avoid the following scenario
- 13:00
- 15:00
- 14:00
Any suggestions on how I should d开发者_运维问答eal with this?
Because of the way you are representing the times as strings, you can simply compare the strings to see whether each string is greater (or equal to, depending on preference) the previous. However, this does rely on, say, 9am being written with a leading zero as "09:00".
simply parse it to time and compare it with entered time if you find it invalid than prompt user to enter valid data.
You can parse using SimpleDateFormat Class into date than you can ofcourse compare two date.
This is some pseudo-code that should do what you want.
List times = new ArrayList();
previousTime = time.get(0);
for(time : times)
{
if(time < previousTime)
return false;
previousTime = time;
}
The idea is to loop through the values and check to make sure that each one is greater than or equal to the previous time.
精彩评论