Thursday, March 19, 2009

How do I format a Date as a String?

Either use the format() method of the SimpleDateFormat class, or the (static) format() method of the String class.



DateFormat dateFormat = new SimpleDateFormat("hhmmddMMyy");

Date today = new Date();

String formatted = dateFormat.format(today);

String formatted2 =

String.format("%<tH%<tM%<tS%tY%<tm%<td", today);


No comments: