novicePrgrmr novicePrgrmr - 1 year ago 151
Java Question

Convert java.util.Date to String

I want to convert a

object to a
in Java.

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

Answer Source

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);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download