Time Format Example

This example shows how to format time using Format class. In this program we use a pattern of special characters to time format.

Time Format Example

This example shows how to format time using Format class. In this program we use a pattern of special characters to time format.

Time Format Example

Time Format Example

     

This example shows how to format time using Format class. In this program we use a pattern of special characters to time format.

Description of the code :

SimpleDateFormat() : SimpleDateFormat class use for formatting and parsing dates. It allows for formatting date into text , parsing text into date and normalization.

format() : This method used for format Date class object into date / time and appends the it into the StringBuffer.

FormattingTime.java
importjava.text.*;
importjava.util.*;

publicclassFormattingTime{

publicstaticvoidmain(Stringargs[]){
Locale[]locales={Locale.FRANCE,Locale.ITALY,Locale.CHINA};

Datedate=newDate();
Strings;

for(inti=0;i<locales.length;i++){

System.out.println("Dateformatein"+locales[i].getDisplayLanguage());
//22.33
s=SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT,locales[i]).format(date);
System.out.println("Short\t:"+s);

//22.33.03
s=SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,locales[i]).format(date);
System.out.println("Medium\t:"+s);

//22.33.03PST
s=SimpleDateFormat.getTimeInstance(SimpleDateFormat.LONG,locales[i]).format(date);
System.out.println("Long\t:"+s);

//22.33.03PST
s=SimpleDateFormat.getDateInstance(SimpleDateFormat.FULL,locales[i]).format(date);
System.out.println("Full\t:"+s);

//22.33.03
s=SimpleDateFormat.getTimeInstance(SimpleDateFormat.DEFAULT,locales[i]).format(date);
System.out.println("Default\t:"+s+"\n");
}
}
}



Output

Date formate in French
Short  : 15:55
Medium  : 15:55:00
Long  : 15:55:00 IST
Full  : samedi 11 octobre 2008
Default  : 15:55:00

Date formate in Italian
Short  : 15.55
Medium  : 15.55.00
Long  : 15.55.00 IST
Full  : sabato 11 ottobre 2008
Default  : 15.55.00

Date formate in Chinese
Short  : ???3:55
Medium  : 15:55:00
Long  : ???03?55??00?
Full  : 2008年10??11? ?????
Default  : 15:55:00

Download code