Wednesday, March 4, 2009

Date Format

Before going learn about Date Format We have to know the Locale class.

DateFormat:
The representation of date is varied from location to location ,we can format the date for a specific locale by using date format class.It is available in java.text.package.It is an abstract class we are not allowed to create an instance of date format by using constructor.
DateFormat d=new DateFormat() -------->not valid
Getting DateFormat object for the default locale:
public static DateFormat getInstance();
public static DateFormat getDateInstance();
public static DateFormat getDateinstance(int style);
the allowed styles are :
DateFormat.FULL(0);
DateFormat.LONG(1);
DateFormat.MEDIUM(2);
DateFormat.SHORT(3);
Getting DateFormat Object for the specific locale:
public static DateFormat getDateInstance(int style,Locale l)
Formating and parsing date:
Public String format(Date d) ----->from java date format to locale specific date format
Public Date parse(String s) throws parseException //from locale specific format to date format.

Example Date Format:
Import java.text.*;
Import java.util.*;
Class DateFormatDemo
{
public static void main(String arg[])
{
DateFormat d=DateFormat.getDateInstance(0);
System.out.println(“Full form is:”+d.format(new date());
System.out.println(“lond form;”+(DateForamt.getDateInstance(1)).format(new Date());
System.otu.println(“medium is:”); //Feb 3, 2008
System.out.println(“short is”); // 2/3/08
System.otu.println(“Full is:”); //Sunday,febravary3, 2008
System.out.println(“Long is”); // Febravary 3 ,2008
}

Demo:
For India,italy
DateFormat in=DateFormat.getDateInstance(0,new Locale(‘pa”,”In”);
DateFormat us=DateFormat.getDateInstance(0);
DateForamt it=DateFormat.getDateInstance(0,new Locale.ITALY);
System.out.println(“India style:”+in.format(new Date()); // Sunday,febravary 3,2008
System.out.println(“us style:”+us.format(new Date()); // Sunday Febravary 3, 2008
System.out.println(“Italy style:”+it.format(new Date()); // dominica 3,febravary 2008.
Getting DateFormat object for representing both date and time.
Public static DateFormat getDateTimeInstance();
Public static DateFormat getDateTimeInstance(int date style, int time style);
Public static DateForamt getDateTimeInstance(int date style, int time style, locale l);

Demo:
DateFormat in=DateFormat.getDateTimeInstance(0,0,Locale.UK);
System.out.println(in); // 03 , febravary 2008 12:10:50 0’clock IST
Us: Sunday febravary 3,2008 12:10:50 pm IST

0 comments:

Post a Comment