Customizing the Number Formats
October 11, 2009 Leave a comment
While working in my college project, I required the selected date from database to be display in a way that day and month should be in two digits (i.e. 01-05-1995). For that I had found some solution among which I like the two methods much.
The first one is constructing the pattern, where we use the formatting property of DecimalFormat with a pattern string. Then We send the pattern in DecimalFormat constructor and a double value in format() function.
Here is a small example which had fulfilled my requirements.
public class GetNumFormat {
public static void main(String[] args)
{
int date[]={1,3,1995};
java.text.DecimalFormat nft = new java.text.DecimalFormat(“#00″);
//nft.setDecimalSeparatorAlwaysShown(false)
System.out.println(nft.format(date[0])+”-”+nft.format(date[1])+”-”+date[2]);
int x = 1;
System.out.println(String.format(“%02d”, date[0])+”-”+
String.format(“%02d”, date[1])+”-”+
String.format(“%02d”, date[2]));
}
}
And in second method, We can change the format using C like formatting using String function format(), which i have implemented in second print statement.
To get more detailed knowledge in pattern construction I would like you to follow sun tutorial in http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

Recent Comments