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
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
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