jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Can I use String.format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can I use String.format?" Watch "Can I use String.format?" New topic

Can I use String.format?

thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Hi Everybody,
My GUI accepts a string from the user i.e the time offset...it should be in the format +/-hh:mm...but a bug has been reported because there is apparently no check for the format...it is accepting even a string like "abcd",but not performing(obviously!) the required conversion.

Can anyone suggest how to accept a string only in the form of +/-hh:mm and prompt the user to reenter in case a wrong entry is made?I looked under formatter class,but im totally confused how to use this effectively...

Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
You said GUI, I think you should read up on how to enforce a mask, so the user can enter nothing else.
As for formatting, sorry parsing the String you have, cos you'll need to do it on the back end regardless, look at SimpleDateFormat. I haven't used it for a time like yours but it might do the trick. Worth a shot.
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Thank you Ta Ri Ki Sun...im working on tht now...
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Hello everyone,
Im still not able to fix this one... Suppose i have to take a user supplied string(time offset) and validate whether it is of the form +/-hh:mm.I have used the following:

DateFormat formatter = new SimpleDateFormat("Z");//is this possible?

try {
Date date = (Date)formatter.parse("-01:00");
} catch(ParseException e) {
System.out.println("Exception has occurred...");

The problem is exception is thrown even for valid inputs like the one above,so obviously exception is thrown for something like "abcd"...can somebody tell me why this is happening?Is there any other way to validate the offset?

Any help will be appreciated...
[ November 20, 2005: Message edited by: thejaswini ramesh ]
santosh kulkarni

Joined: Jul 21, 2004
Posts: 18
java almanac
has lots of examples about DateFormat

Impossible is not a fact its an opinion
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I just had to do a something similar for an application I put together. Since the date input is from a web page, it is easier for me to check it on the back end (although it's probably not the best way). I know there are ways to do masks in javascript... but I digress.

First was to check the input length.. if it wasn't 8 characters, then I kicked it back.

I needed the input date to be in the format of mm/dd/yy, so I put together an admittedly crude method to check that it was a date. Basically, I split the string at the backslashes and checked the token count. If there was not three, then it wasn't valid, and I kicked it back to the user. Then I checked to see if the tokens contained digits using regular expressions. If there were any letters, I kicked it back to the user.

Then I wanted to verify the ranges. I wanted to make sure that the months were in the range 01-12, so I added a zero if necessary, and checked the range. If it was out of bounds, I kicked it back. Then I did the same with the days.. 1-31. The dates that get entered are based on when paperwork is completed, so if they put in February 31, it's because of a typo. I could pretty easily check range of dates based on the month, but as I said, I'm not too worried about it.

I didn't put a range restriction on the years at all. None of it is very pretty, but it is just a short method that does the job. It doesn't have to handle that many dates at a time, so I am not too concerned about efficiency.

Hmm.. now that I think about it, I have a snipet of code stashed somewhere that checks the input, and if it isn't a digit, it consumes it..

I can't remember where I grabbed that from, but it isn't mine.. I have used it dozens of times though. You will still have to check for proper length, and that the slashes are in the proper spot using this method.

Anyway, that's two ways I know to skin this cat.
[ November 22, 2005: Message edited by: C Clites ]
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Thank you santosh & Clites...i have been working on tht pbm(yes java almanac has been of great help... )...i modified to suit my requiremnt i,e check if the time offset enetered by the user is in the rt format..+/-hh:mm

This is how i have implemented...even i have tokenized the string and checked for the ":"...i completely agree...my code doesn't look any prettier,but it seems to the job...any suggestions on how to improve it will be apprecieted.


:roll: :roll: :roll: :roll: :roll:
[ November 22, 2005: Message edited by: thejaswini ramesh ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Can I use String.format?
It's not a secret anymore!