Validating 4 digit string as time?

Is there an easy way to validate if a four char string is a valid time?

ie. 08:00 is 8h00 but 80:00 is not valid?

Thnx in Advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What you could do is use a substring of the 4 character string and cast that to an int and chaeck to see if it is greater than 24 for the hours and 59 for the minutes. Here's a sample.

String time = "08:00";

int hour = Integer.parseInt(time.substring(0, 2));

int hour = Integer.parseInt(time.substring(3, 5));

the validate the times using.

if(hour > 24)
  System.out.println("Invalid time");

if(min > 59)
  System.out.println("Invalid time");


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oops mistake. Rename the second variable hour to min.


not simple way:
use Regular expressions :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.