Topics:

  1. Introduction
  2. Locale Class
  3. NumberFormat Class
  4. DateFormat Class

Introduction

The process of developing web application that supports various languages and various regions with out making any changes in the application.

we can achieve this by using following classes.

Class Description
Locale To represent a particular region
NumberFormat For formatting Numbers
DateFormat For formatting Dates

Locale Class

  1. A Locale object represents a particular region with respect to country (or) language.
  2. It is a final class available in java.util package and implements Serializable and Clonable interfaces.

Constructors of Locale class.

We can construct the Locale Object by using the following Locale class constructor.

  1. Locale l = new Locale(String Language);
  2. Locale l = new Locale(String Language, String Country);

Locale class already defined some standard locale objects in the form of constants.Based on our requirement we can use these Locale objects directly.

Example:
Locale.UK,Locale.ITALY,Locale.CHINA.

Important methods of Locale Class

  1. public static Locale getDefault()
    Returns the default locale configure in JVM.
  2. public static void setDefault(Locale l)
    To set our own Locale.
  3. public String getCountry()
  4. public String getDisplayCountry()
  5. public String getLanguages()
  6. public String getDisplayLanguages()
  7. public static String[] getISOCountries()
    Returns ISO countries supported by the JVM.
  8. public static String[] getISOLanguages()
  9. public static Locale[] getAvailableLocales()

Example:

import java.util.Locale;

class Test {

	public static void main(String[] args) {

		Locale l = Locale.getDefault();
		System.out.println(l.getCountry() + "..." + l.getLanguage());
		System.out.println(l.getDisplayCountry() + "..."+ l.getDisplayLanguage());
		Locale l2 = new Locale("pa", "IN");
		Locale.setDefault(l2);
		String s3[] = Locale.getISOLanguages();
		for (String s4 : s3) {
			System.out.println(s4);
		}
		String s5[] = Locale.getISOCountries();
		for (String s6 : s5) {
			System.out.println(s6);
		}
		Locale l3[] = Locale.getAvailableLocales();
		for (Locale l4 : l3) {
			System.out.println(l4);
			System.out.println(l4.getDisplayCountry() + "....."	+ l4.getDisplayLanguage());
		}
	}

}

NumberFormat Class

  1. Different countries follow different conventions to represent numbers.
  2. We can format a number according to a particular country by using NumberFormat class.
  3. NumberFormat class is available in java.text package and it is abstract class.

Getting NumberFormat object for default locale

NumberFormat class contains the following factory methods to get NumberFormat Object.

  1. public static NumberFormat getInstance();
  2. public static NumberFormat getCurrencyInstance();
  3. public static NumberFormat getPercentInstance();
  4. public static NumberFormat getNumberInstance();

Getting NumberFormat Object for a particular locale.

  1. We have to pass the corresponding Locale object as the argument to the above methods.

    public static NumberFormat getCurrencyInstance(Locale l)

  2. NumberFormat class contains the following methods for converting a java number to the Locale Specific number format.

    String format(long l)
    String format(double d)

  3. NumberFormat class contains the following method for converting Locale specific form to java Number Format
    Number parse(String s) throws ParseException.

Example:

import java.text.NumberFormat;
import java.util.Locale;

class Test {

	public static void main(String[] args) {

		double d1 = 123456.789;
		Locale india = new Locale("pa","IN");
		NumberFormat nf = NumberFormat.getCurrencyInstance(india);
		System.out.println("India Notation is ....." + nf.format(d1));
		
		NumberFormat nf1 =		NumberFormat.getCurrencyInstance(Locale.ITALY);
		System.out.println("Italy Notation is ....." + nf1.format(d1));
		
		NumberFormat nf2 =NumberFormat.getCurrencyInstance(Locale.CHINA);
		System.out.println("China Notation is ....." + nf2.format(d1));
		
		NumberFormat nf3 =NumberFormat.getCurrencyInstance(Locale.US);
		System.out.println("US Notation is ....." + nf3.format(d1));
	}

}
/*
Output:
India Notation is .....INR 123,456.79
Italy Notation is .....€ 123.456,79
China Notation is .....?123,456.79
US Notation is .....$123,456.79
*/

Setting max/min integer and fraction digits

NumberFormat class contains the following methods for specifying max and min fraction and integer digits.

  1. public void setMaximamIntegerDigits(int n)
  2. public void setMinimumIntegerDigits(int n)
  3. public void setMaximamFractionDigits(int n)
  4. public void setMinimumFractionDigits(int n)

Example:

import java.text.NumberFormat;

class Test {

	public static void main(String[] args) {

		NumberFormat nf = NumberFormat.getInstance();

		nf.setMaximumIntegerDigits(4);
		System.out.println(nf.format(123456.789));

		nf.setMinimumIntegerDigits(4);
		System.out.println(nf.format(12.456));

		nf.setMaximumFractionDigits(2);
		System.out.println(nf.format(123456.789));

		nf.setMinimumFractionDigits(3);
		System.out.println(nf.format(123.4));
	}

}
/*
Output:
3,456.789
0,012.456
3,456.79
0,123.400

*/

DateFormat Class

  1. DateFormat class can be used for formatting the dates according to a particular locale.
  2. DateFormat class is available in java.text package.
  3. DateFormat class is an abstract class we can’t create an object by using the constructor.

Creation of DateFormat Object for a default Locale.

DateFormat class contains the following methods to get DateFormat Objects.

  1. public static DateFormat getDateInstance()
  2. public static DateFormat getDateInstance()
  3. public static DateFormat getDateInstance(int style)

Where style is

0-DateFormat.FULL 
1-DateFormat.LONG
2-DateFormat.MEDIUM
3-DateFormat.SHORT

Creation of DateFormat Object for a specific Locale

public static DateFormat getDateInstance(int style, Locale l)

  1. DateFormat class contain the following method for converting java Date form to Locale specific format.

    String format(Date d)

  2. DateFormat class contain the following method for converting Locale Specific form to java Date format.

    Date parse(String s)throws ParseException

Example:

import java.text.DateFormat;
import java.util.Date;

class Test {

	public static void main(String[] args) {

		System.out.println("Full form is --"
				+ DateFormat.getDateInstance(0).format(new Date()));
		System.out.println("Long form is --"
				+ DateFormat.getDateInstance(1).format(new Date()));
		System.out.println("Medium form is --"
				+ DateFormat.getDateInstance(2).format(new Date()));
		System.out.println("Short form is --"
				+ DateFormat.getDateInstance(3).format(new Date()));
	}

}
/*
Output:
Full form is --Tuesday, July 29, 2014
Long form is --July 29, 2014
Medium form is --Jul 29, 2014
Short form is --7/29/14
*/

Creation of DateFormat Object for representing both Date and Time
The following are the constructors for representing both Data and Time.

  1. public static DateFormat getDateTimeInstance()
  2. public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
  3. public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale l)