View RSS Feed

My Java Tips

DateFormat Class

Rate this Entry
by , 11-03-2011 at 12:00 PM (634 Views)
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.




Java Code:
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

Submit "DateFormat Class" to Facebook Submit "DateFormat Class" to Digg Submit "DateFormat Class" to del.icio.us Submit "DateFormat Class" to StumbleUpon Submit "DateFormat Class" to Google

Tags: None Add / Edit Tags
Categories
Java SE

Comments