File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with Time Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Time Validation" Watch "Help with Time Validation" New topic
Author

Help with Time Validation

Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
I am researching the best/easiest way to validate if a string contains a valid TIME (ie. hh.mm.ss) Some people have suggested that I add a date to it and use some of the DATE(), or Calandar() classes to extract the time, but I don't see this an effective method. I'm looking for something that would act like "String.isTime(testString)" if it existed for example. Since this string will be used to set an instance variable, I have to be certain it's a valid time.

Any help would be greatly appreciated.


Lou Bassett
Pavan Panduga
Ranch Hand

Joined: Jun 12, 2002
Posts: 68
how about Regular Expressions? you could match the pattern you're looking for.
Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
Thanks for the tip.

I looked at Regular Expressions, specifically the regex.api. It appears that it can reckonize a specific pattern but it would fall short on validation. For example.. 34:99.12 (Pattern is OK, but invalid time)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Take a look at java.text.SimpleDataFormat.

Layne
[ November 29, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
Thanks everyone.

Could someone provide some code that would accept an string()and validate:

1. In hh.mm.ss format and
2. A real time.

Ideally, the method would return boolean.

Thanks for all your help.
Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13


I need help with this. Any help would be greatly appreciated.

Thanks
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
and Layne provided help

take a look at the javadocs for Simple Date Format build a format string similar to "HH.mm.ss" and use it to construct your DateFormat (as a SimpleDateFormat) and then use the parse method from the DateFormat - catch any FormatExceptions) and you'll know whether or not you have a valid time.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Lou Bassett:


I need help with this. Any help would be greatly appreciated.

Thanks


Did you even read my message above? Did you follow the link I gave and read that web page? If you don't understand the information given, feel free to ask more specific questions. I apologize if I'm coming across a bit strong, but I'm a bit irked that you say you need help when we have given plenty. As you can see, we are not in the business of writing code for others, especially when it seems like it could be part of a homework assignment. Please show some effort on your part. If you get stuck with a particular concept, you should demonstrate what you have tried so far, what results you got, and explain how the results differ from what you expect. If you can do that much, there are lots of people around here that are more than willing to fill in the gaps.

I wish you best of luck with our project. Be sure to check out the link for SimpleDateFormat that I gave above.

Regards,

Layne
Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
Layne,

I've been an AS/400 programmer for over 20 years. If it can be done on the iSeries, I can do. Management basically told me to drop everything, learn Java, write and maintain existing code in production. The two Java programmers/contractors here left - without giving notice - and now I'm responsible. Its not a homework assignment, it's real life, real time and real money.

Yes, I read what you wrote. What I didn't quickly pick up on was the use additional validation of the "H" vs "h" in the format. Now I do. The following is what I coded. Once again, thank you for your assistance. It was greatly appreciated.



Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I apologize for getting on my soap box. As you probably see, it's quite trivial once you understand all the details. There are plenty of college students that come around here that want programs written for them without trying to learn it themselves. Anyways, enough of that.

As you are working on Java, the Java API docs will be an invaluable resource. Of course, you may have figured that out already. Unfortunately, you have to have a vague idea where to look before you can find it.

If you don't mind me asking, is your company looking to replace those Java programmers? I'm currently in school, but I am graduating in May with a Bachelor's in Computer Science and Mathematics. I have a fair amount of experience with Java as well. Anyway, I just thought I'd ask. In the mean time, good luck with the program. Don't hesitate to come back with more questions. I'll try to be less harsh on you

Layne
Lou Bassett
Greenhorn

Joined: Nov 08, 2004
Posts: 13
No Problem.

I work for Syndicated Office Systems (IS group for Tenet Healthcare) in Anaheim Ca. They're not looking right now - hence my being thrown into the lion's den. I think they'll open it up in 2005 if the user community yells loud enough for more Java-type stuff. I also have many contact names/head-hunters (the good one's) for when you you start looking (Southern Cal).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with Time Validation