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.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
import java.text.*;
import java.util.*;
public class FormattingTime {
public static void main(String args[]) {
Locale[] locales = {Locale.FRANCE, Locale.ITALY, Locale.CHINA};
Date date = new Date();
String s;
for (int i = 0; i < locales.length; i++) {
System.out.println("Date formate in " + 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.03 PST
s = SimpleDateFormat.getTimeInstance(SimpleDateFormat.LONG, locales[i]).format(date);
System.out.println("Long\t: " + s);
// 22.33.03 PST
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
|
Download code


