views:

35

answers:

3

Hi,

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

  1. 13:00
  2. 15:00
  3. 14:00

Any suggestions on how I should deal with this?

+1  A: 

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.

Alan Geleynse
+2  A: 

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".

Tim
+3  A: 

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.

org.life.java