Saturday, October 23, 2010

java.text.SimpleDateFormat

DateFormat dateFormat = new SimpleDateFormat("");

For 20th October, 2010 13:40:19.333, that is 20th October, 1:40 PM and 19 seconds with 333 seconds, the Java mapping would be.

yyyy = 2010
yy = 10 (instead of 2010 only 10 would be printed. Don't forget y2k problem)
MM - 10 (10th month of the georgean calendar)
dd - 20 (20th day of October month)
HH/hh - 01 (should print hour part of date format)
mm - 40 (minute part of the date part)
ss - 10 (seconds of the date part)
SSS - 333 (milliseconds part of the date format)

Usage:

Date d = new Date();
DateFormat datefromat = new SimpleDateFormat("yyyyMMdd HH:mm:ss SSS");
String dateAsString = dateformat.format(d);
System.out.println("The date in text format is:" + dateAsString);

Imports:

java.util.Date
java.text.SimpleDateFormat

No comments:

Post a Comment