From Suhrid.net Wiki
Revision as of 05:16, 5 September 2011 by Suhridk (talk | contribs) (→‎Locale Class)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


  • J2SE APIs allow us to format dates, numbers and currencies in a locale specific manner.
  • The important classes are java.util.Date, java.util.Calendar, java.text.DateFormat, java.text.NumberFormat and java.util.Locale.

Date Class

  • An instance of the date class represents a date and time.
  • The no-arg constructor will initialize a date object to the current system time.
  • Not really used for i18n and localization.
  • Most of the methods are deprecated, use a Calendar class instead.

Calendar Class

  • The calendar class makes date manipulation easy, like adding days to a date.
  • Calendar class is abstract, so an instance can be obtained throught is static factory methods.
  • The calendar class has convenience methods to set the date and time in an easy way. cal.set(int year, int month, int day) etc.
  • Example:
import java.util.Calendar;
import java.util.Date;

public class TestDate {

	public static void main(String[] args) {
		Date d = new Date();
		Calendar c = Calendar.getInstance();
		System.out.println("Now : " + c.getTime());
                //Now : Sat Jul 02 15:53:02 IST 2011
		c.add(Calendar.MONTH, 3);
		System.out.println("After 3 months : " + c.getTime());
                //After 3 months : Sun Oct 02 15:53:02 IST 2011
  • When adding, the calendar will normalize the values if they are larger. For e.g. if we are adding 13 months to a date, 13 will be normalized to 1 year and 1 month.


  • Calendar class has a roll method which does not add the larger components of the date. For e.g. if we are incrementing the month in December, the year will not roll over to the next.
  • Example:
public class DateRoll {

	public static void main(String[] args) {
		DateFormat df = DateFormat.getDateInstance();
		Date d = null;
		try {
			d = df.parse("25 Dec, 2011");
		} catch (ParseException e) {
		Calendar cal = Calendar.getInstance();
		cal.roll(Calendar.MONTH, 1); 
                //This will print : Tue Jan 25 00:00:00 IST 2011
                //cal.add would have printed: Wed Jan 25 00:00:00 IST 2012	

DateFormat Class

  • Used to format dates according to date styles (short, medium etc) and Locales.
  • DateFormat is abstract, so instance can be obtained using a static factory method.
  • the format() method is used for formatting.
  • the parse() method is to parse String's into Date objects. Throws a checked ParseException.
  • See example above. The string has to be in the correct locale-specific format, or the method will throw a ParseException.
  • three different ways of constructing a DateFormat object:
    • DateFormat.getDateInstance() - used to format dates.
    • DateFormat.getTimeInstance() - used to format times.
    • DateFormat.getDateTimeInstance() - used to format both dates and times.
    • DateFormat.getInstance() - returns a SHORT DateTimeInstance.
    • The above methods take an (int style, Locale loc) or only (int style) with the default locale being used.
  • example format:
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatTest {

	public static void main(String[] args) {
		Date d = new Date();
		DateFormat shortDateUK = DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK);
		//Will print: 04/07/11
		DateFormat longDateUS = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
		//Will print: July 4, 2011
		DateFormat shortTimeUK = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.UK);
		//Will print: 15:12
		DateFormat longTimeUS = DateFormat.getTimeInstance(DateFormat.LONG, Locale.US);
		//Will print: 3:12:46 PM IST
		DateFormat shortDateTimeUK = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK);
		//Will print: 04/07/11 15:12
		DateFormat longDateTimeUS = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
		//Will print: July 4, 2011 3:12:46 PM IST

NumberFormat Class

  • In different contries, numbers are formatted in different ways (for e.g usage of commas).
  • Used to format numbers and currency according to certain conditions and the locale.
  • It is also an abstract class

Formatting Numbers

  • Things like max/min fraction digits and integer digits can be set and numbers can be formatted according to the locale.
  • getInstance() is the same as getNumberInstance()
  • See example below:
public class NumberFormatTest {

	public static void main(String[] args) throws ParseException {
		float f = 123.45678f;
		NumberFormat nf = NumberFormat.getInstance();
		System.out.println(nf.format(f)); //Prints 123.46
		System.out.println(nf.parse("3.14")); //Prints 3
		NumberFormat nfUK = NumberFormat.getInstance(Locale.UK);
		NumberFormat nfIT = NumberFormat.getInstance(Locale.ITALY);
		long million = 1000000;
		System.out.println(nfUK.format(million)); //Prints 1,000,000
		System.out.println(nfIT.format(million)); //Prints 1.000.000

Formatting Currency

  • For currency, use NumberFormat.getCurrencyInstance()
public class CurrencyTest {

	public static void main(String[] args) {
		int hundred = 100;
		NumberFormat curUS = NumberFormat.getCurrencyInstance(Locale.US);
		System.out.println(curUS.format(hundred)); //Prints $100
		NumberFormat curJP = NumberFormat.getCurrencyInstance(Locale.JAPAN);
		System.out.println(curJP.format(hundred)); //Prints ¥100


Parsing Strings to Numbers

  • The NumberFormat class defines a parse() method which is sort of reverse of the format method.
  • It accepts as a String as input and returns a Number. This is done in a Locale aware manner.
  • The parse() method throws a checked ParseException.
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class NFParse {

	public static void main(String[] args) {
		NumberFormat nf = NumberFormat.getInstance(Locale.US);
		String s = "1,23,467.38";
		try {
			Number n = nf.parse(s);
			double d = n.doubleValue();
			System.out.println("double : " + d); //Prints 123467.38
			int i = n.intValue();
			System.out.println("int : " + i); //Prints 123467
		} catch (ParseException pe) {


Locale Class

  • The locale class is used to represent a geographical, political, or cultural region.
  • Setting the correct locale is the key to localization.
  • Two ways to construct locale
  • Locale(String language);
  • Locale(String language, String country);
  • There exists constants defined for language and country locales, but custom locales can be specified using the ISO language code.
  • For e.g. Locale.ENGLISH is for the english langauge and Locale.UK is for the country - UK uses english, but date settings (dd/mm/yy) differs that from US locale which also uses english.
public class LocaleTest {

	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		cal.set(2011, 07, 02);
		Date d = cal.getTime();
		DateFormat dfIT = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALIAN);
		System.out.println(dfIT.format(d)); // martedì 2 agosto 2011
		DateFormat dfEN = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH);
		System.out.println(dfEN.format(d)); // Tuesday, August 2, 2011
		DateFormat dfUK = DateFormat.getDateInstance(DateFormat.FULL, Locale.UK);
		System.out.println(dfUK.format(d)); // Tuesday, 2 August 2011
		DateFormat dfUS = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
		System.out.println(dfUS.format(d)); // Tuesday, August 2, 2011


  • The locale class has methods to get the display name of the country and the language for a Locale object.
  • The getDisplayCountry() and getDisplayLanguage() return the country and language name of the locale object localized as per the current JVM locale.
  • You can also translate a locale's country and language into another locale by using getDisplayCountry(Locale locToTranslateTo);
public class DisplayCountryLang {

	public static void main(String[] args) {
		System.out.println("In the UK : " + Locale.UK.getDisplayCountry() + "," + Locale.UK.getDisplayLanguage()); 
                //Prints - In the UK : United Kingdom,English
		System.out.println("UK according to the French : " + Locale.UK.getDisplayCountry(Locale.FRANCE) + ", " + Locale.UK.getDisplayLanguage(Locale.FRANCE)); 
                //Prints - UK according to the French : Royaume-Uni, anglais
  • See below example with the default locale being set to France. So Locale.US, language and country will be displayed in the current JVM locale in French.
  • If we want the US Locale's country and language information, we should use Locale.US as the argument.
import java.util.Locale;

public class Lokale {

	public static void main(String[] args) {
		Locale us = Locale.US;
		System.out.println(us.getDisplayLanguage()); //anglais
		System.out.println(us.getDisplayCountry());  //Etats-Unis
		System.out.println(us.getDisplayCountry(us)); //United States
		System.out.println(us.getDisplayLanguage(us)); //English