Customizing the Number Formats

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″);



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