File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes phone number formatter in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "phone number formatter in struts2" Watch "phone number formatter in struts2" New topic

phone number formatter in struts2

Sandeep Kumar.

Joined: Jul 23, 2008
Posts: 9

I have to display a phone number in (xxx)xxx-xxx format while i'm getting the phone number from the db as a 10 digit number(e.g. - 1324527865)

There are a lot of example on formatting date and numbers but no example on formatting strings(inserting comma,dot etc).

Any help will be much appreciated.

Mike Cobelmach

Joined: Jun 17, 2009
Posts: 19
You can use static method which convert string from database to your phone number format. More informations are in
Roberto Hernandez
Ranch Hand

Joined: Apr 29, 2009
Posts: 33
If you have a string, the best thing to do is to create a method that uses Java String Manipulation to convert to the format you want. In this case, you'll have to insert a parenthesis ( at the first position, ) at the third position and/or a hyphen - at the 4th position (999) 999-999. It shouldn't be that complicated. Use google and you'll find some examples
Sandeep Kumar.

Joined: Jul 23, 2008
Posts: 9
I could always write a java code for masking and unmasking phone number which is what i am doing for time being.

but i wanted to define some format in ResourceBundle (e.g. item.price={0,number,currency}) so that i don't have to do the conversion by myself every time.

struts uses java.text.MessageFormat class for formatting and i don't know how to use MessageFormat class for string formatting .

still waiting for help.......
I agree. Here's the link:
subject: phone number formatter in struts2
It's not a secret anymore!