novicePrgrmr novicePrgrmr - 2 months ago 21
Java Question

Convert java.util.Date to String

I want to convert a

java.util.Date
object to a
String
in Java.

The format is
2010-05-30 22:15:52

Answer

In Java, Convert a Date to a String using a format string:

// Create an instance of SimpleDateFormat used for formatting 
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

// Get the date today 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 reportDate = df.format(today);

// Print what date is today!
System.out.println("Report Date: " + reportDate);

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

Comments