Convert java.util.Date to String Convert java.util.Date to String java java

Convert java.util.Date to String


Convert a Date to a String using DateFormat#format method:

String pattern = "MM/dd/yyyy HH:mm:ss";// Create an instance of SimpleDateFormat used for formatting // the string representation of date according to the chosen patternDateFormat df = new SimpleDateFormat(pattern);// Get the today date using Calendar object.Date today = Calendar.getInstance().getTime();        // Using DateFormat format method we can create a string // representation of a date with the defined format.String todayAsString = df.format(today);// Print the result!System.out.println("Today is: " + todayAsString);

From http://www.kodejava.org/examples/86.html


Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String s = formatter.format(date);


Commons-lang DateFormatUtils is full of goodies (if you have commons-lang in your classpath)

//Formats a date/time into a specific pattern DateFormatUtils.format(yourDate, "yyyy-MM-dd HH:mm:SS");