Localization

From Suhrid.net Wiki
Revision as of 03:24, 2 July 2011 by Suhridk (talk | contribs)
Jump to navigationJump to search

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
		
	}
}