views:

100

answers:

3

how to convert current date into string in java?

+2  A: 
new Date().toString();
jasonmp85
This will show the date and time, not just the current date.
Adamski
Well sorry for overachieving.
jasonmp85
+3  A: 

Use a DateFormat implementation; e.g. SimpleDateFormat.

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.err.println(df.format(new Date()));
Adamski
+5  A: 
// GET DATE & TIME IN ANY FORMAT
import java.util.Calendar;
import java.text.SimpleDateFormat;
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";

public static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}

Taken from here

Itay
Best response for using Calendar instead of Date.
Random
I'm confused. In what universe is `Calendar` more desirable than `Date`?
jasonmp85