Difference between revisions of "Localization"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 101: Line 101:
  
 
* Used to format numbers and currency.
 
* Used to format numbers and currency.
 +
* It is also an abstract class
  
 
=== Formatting Numbers ===
 
=== Formatting Numbers ===
Line 129: Line 130:
 
}
 
}
  
 +
 +
</syntaxhighlight>
 +
 +
* For currency, use NumberFormat.getCurrencyInstance()
 +
 +
<syntaxhighlight lang="java5">
 +
 +
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
 +
 +
}
 +
 +
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 05:44, 2 July 2011

Introduction

  • 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.
  • 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();
		c.setTime(d);
		
		System.out.println("Now : " + c.getTime());
                //Prints:
                //Now : Sat Jul 02 15:53:02 IST 2011
		
		c.add(Calendar.MONTH, 3);
		
		System.out.println("After 3 months : " + c.getTime());
                //Prints:
                //After 3 months : Sun Oct 02 15:53:02 IST 2011
		
	}
}

Rolling

  • 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) {
			e.printStackTrace();
		}
		
		Calendar cal = Calendar.getInstance();
		
		cal.setTime(d);
		System.out.println(d);
		cal.roll(Calendar.MONTH, 1); 
		System.out.println(cal.getTime());
                //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. See example above. The string has to be in the correct locale-specific format, or the method will throw a ParseException.
  • example format:
Date d = new Date();
DateFormat shortUK = DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK);

System.out.println(shortUK.format(d));
//Will print: 02/07/11

DateFormat longUS = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);

System.out.println(longUS.format(d));
//Will print: July 2, 2011

NumberFormat Class

  • Used to format numbers and currency.
  • It is also an abstract class

Formatting Numbers

  • Things like max fraction digits can be set and numbers can be formatted according to the locale. See example below:
public class NumberFormatTest {

	public static void main(String[] args) throws ParseException {
		
		float f = 123.45678f;
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		System.out.println(nf.format(f)); //Prints 123.46
		
		nf.setParseIntegerOnly(true);
		System.out.println(nf.parse("3.14")); //Prints 3
		
		NumberFormat nfUK = NumberFormat.getInstance(Locale.UK);
		NumberFormat nfUS = NumberFormat.getInstance(Locale.ITALY);
		long million = 1000000;
		
		System.out.println(nfUK.format(million)); //Prints 1,000,000
		System.out.println(nfUS.format(million)); //Prints 1.000.000
		
	}
}
  • 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
		
	}

}