Locale-specific formatting in Java

Java 8Object Oriented ProgrammingProgramming

For locale-specific formatting, firstly import the following packages.

import java.util.Calendar;
import java.util.Formatter;
import java.util.Locale;

Create a Formatter and Calendar object −

Formatter f = new Formatter();
Calendar c = Calendar.getInstance();

We are formatting for different Locales −

f.format(Locale.TAIWAN, "Locale.TAIWAN: %tc\n", c);
f.format(Locale.ITALY, "Locale.ITALY: %tc\n", c);

The following is an example −

Example

 Live Demo

import java.util.Calendar;
import java.util.Formatter;
import java.util.Locale;
public class Demo {
   public static void main(String args[]) {
      Formatter f = new Formatter();
      Calendar c = Calendar.getInstance();
      f.format(Locale.TAIWAN, "Locale.TAIWAN: %tc\n", c);
      f.format(Locale.ITALY, "Locale.ITALY: %tc\n", c);
      f.format(Locale.FRENCH, "Locale.FRENCH: %tc\n", c);
      f.format(Locale.GERMANY, "Locale.GERMANY: %tc\n", c);
      System.out.println(f);
   }
}

Output

Locale.TAIWAN: 星期日十一月 25 14:36:20 UTC 2018
Locale.ITALY: dom nov 25 14:36:20 UTC 2018
Locale.FRENCH: dim. nov. 25 14:36:20 UTC 2018
Locale.GERMANY: So Nov 25 14:36:20 UTC 2018
raja
Updated on 30-Jul-2019 22:30:24

Advertisements