Locale Sensitive Operations

This section provides a tutorial example on how to convert date objects to strings in different locales using java.util.SimpleDateFormat class.

Locale Sensitive Operations: Operations that require to know a user's locale in order to tailor information for the user. For example, formatting a calendar date into a string is a locale sensitive operation, because for an English speaking user in US, a calendar date should be formatted as "Mon Oct 21 20:54:05 EDT 2002"; but for a French speaking user, the same date should be formatted as "lun. oct. 21 20:54:05 EDT 2002".

Separated formatter classes are provided to handle locale sensitive operations related to some predefined classes. Here is a list of formatter classes:

Formatter          Data	               Operations
Class              Class

SimpleDateFormat   GregorianCalendar   Formating and parsing
DecimalFormat      Number              Formating and parsing

The following program shows how to internationalize the formatting operation of dates, numbers and currencies:

/* FormatLocaleTest.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
import java.util.*;
import java.text.*;
class FormatLocaleTest {
   public static void main(String[] a) {
   public static void showDateFormat() {
      Date now = new Date(); // the current time
      DateFormat df;
      SimpleDateFormat sdf;
      System.out.println("Default date format:");
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
      System.out.println("   Locale(Default): " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG, Locale.FRANCE);
      System.out.println("   Locale(France): " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG, Locale.FRENCH);
      System.out.println("   Locale(French): " + df.format(now));
      df = DateFormat.getDateTimeInstance(DateFormat.LONG,
         DateFormat.LONG, Locale.GERMAN);
      System.out.println("   Locale(German): " + df.format(now));
      System.out.println("Customized date format:");
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
      System.out.println("   Locale(Default): " + sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", 
      System.out.println("   Locale(French): " + sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", 
      System.out.println("   Locale(German): " + sdf.format(now));
      sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", 
      System.out.println("   Locale(Chinese): " + sdf.format(now));
   public static void showNumberFormat() {
      double num = -1234.5678;
      NumberFormat nf;
      System.out.println("Number format:");
      nf = NumberFormat.getNumberInstance();
      System.out.println("   Locale(Default): "+ nf.format(num));
      nf = NumberFormat.getNumberInstance(Locale.FRANCE);
      System.out.println("   Locale(France): "+ nf.format(num));
      nf = NumberFormat.getNumberInstance(Locale.FRENCH);
      System.out.println("   Locale(French): "+ nf.format(num));
      nf = NumberFormat.getNumberInstance(Locale.JAPAN);
      System.out.println("   Locale(Japanese): "+ nf.format(num));

      System.out.println("Currency format:");
      nf = NumberFormat.getCurrencyInstance();
      System.out.println("   Locale(Default): "+ nf.format(num));
      nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
      System.out.println("   Locale(France): "+ nf.format(num));
      nf = NumberFormat.getCurrencyInstance(Locale.FRENCH);
      System.out.println("   Locale(French): "+ nf.format(num));
      nf = NumberFormat.getCurrencyInstance(Locale.JAPAN);
      System.out.println("   Locale(Japanese): "+ nf.format(num));


Default date format:
   Locale(Default): November 1, 2002 9:45:44 AM EST
   Locale(France): 1 novembre 2002 09:45:44 EST
   Locale(French): 1 novembre 2002 09:45:44 EST
   Locale(German): 1. November 2002 09:45:44 EST
Customized date format:
   Locale(Default): Fri Nov 01 09:45:44 EST 2002
   Locale(French): ven. nov. 01 09:45:44 EST 2002
   Locale(German): Fr Nov 01 09:45:44 EST 2002
   Locale(Chinese): ??? ??? 01 09:45:44 EST 2002
Number format:
   Locale(Default): -1,234.568
   Locale(France): -1á234,568
   Locale(French): -1á234,568
   Locale(Japanese): -1,234.568
Currency format:
   Locale(Default): ($1,234.57)
   Locale(France): -1á234,57 F
   Locale(French): -1á234,57 ñ
   Locale(Japanese): -?1,235

Note that:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

Locales, Localization Methods and Resource Bundles

 java.util.Locale - Localization and Internationalization

Locale Sensitive Operations

 java.util.ResourceBundle - Resource Bundle Abstract Class

 java.util.ListResourceBundle - Resource Bundles as Lists

 java.util.PropertyResourceBundle - Resource Bundles as Properties Files

 java.util.ResourceBundle.getBundle() - Retrieving Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version