DateFormat Class

28 June 2008

DateFormat class is a very useful class included in java.text package that can be used to display date in different formats. Following example will show you how to use it with Date object in meaningful way.

Date now = new Date();
DateFormat df =  DateFormat.getDateInstance();
DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat df4 = DateFormat.getDateInstance(DateFormat.FULL); 
String s =  df.format(now);
String s1 = df1.format(now);
String s2 = df2.format(now);
String s3 = df3.format(now);
String s4 = df4.format(now);
System.out.println("(Default) Today is " + s);
System.out.println("(SHORT)   Today is " + s1);
System.out.println("(MEDIUM)  Today is " + s2);
System.out.println("(LONG)    Today is " + s3);
System.out.println("(FULL)    Today is " + s4);

Output:

(Default) Today is 04.06.2008
(SHORT) Today is 04.06.08
(MEDIUM) Today is 04.06.2008
(LONG) Today is 4. June 2008
(FULL) Today is Friday, 6. June2008

del.icio.us:DateFormat Class  digg:DateFormat Class  spurl:DateFormat Class  wists:DateFormat Class  simpy:DateFormat Class  newsvine:DateFormat Class  blinklist:DateFormat Class  furl:DateFormat Class  reddit:DateFormat Class  fark:DateFormat Class  blogmarks:DateFormat Class  Y!:DateFormat Class  smarking:DateFormat Class  magnolia:DateFormat Class  segnalo:DateFormat Class  gifttagging:DateFormat Class

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: DateFormat Class

Leave a Reply