java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to format date using a locale based format?

The code below demonstrate how to format date information for a specific Locale. In the example utilize the java.text.SimpleDateFormat class.

package com.javacoderanch.example.text;

import java.util.Locale;
import java.util.Date;
import java.text.SimpleDateFormat;

public class FormatDateLocale {
    public static void main(String[] args) {
        // Defines an array of Locale we are going to use for 
        // formatting date information.
        Locale[] locales = new Locale[] {

        // Get an instance of current date time
        Date today = new Date();

        // Iterates the entire Locale defined above and create a long 
        // formatted date using the SimpleDateFormat.getDateInstance() 
        // with the format, the Locale and the date information.
        for (Locale locale : locales) {
            System.out.println("Date format in " 
                + locale.getDisplayName() 
                + " = " 
                + SimpleDateFormat.getDateInstance(
                      SimpleDateFormat.LONG, locale)

The result of our code are:

Date format in Japanese (Japan) = 2009/01/04
Date format in Chinese (China) = 2009年1月4日
Date format in Korean (South Korea) = 2009년 1월 4? (?)
Date format in Chinese (Taiwan) = 2009年1月4日
Date format in Italian (Italy) = 4 GENNAIO 2009
Date format in French (France) = 4 JANVIER 2009
Date format in German = 4. JANUAR 2009