1 Date date = Calendar.getInstance().getTime();
2 DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
3 String strDate = dateFormat.format(date);
1// java convert date to string
2import java.util.Date;
3import java.text.SimpleDateFormat;
4public class DateToStringDemo
5{
6 public static void main(String[] args)
7 {
8 Date dt = new Date();
9 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
10 String strDate = sdf.format(dt);
11 System.out.println("Date format - MM/dd/yyyy to string is : " + strDate);
12 sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
13 strDate = sdf.format(dt);
14 System.out.println("Date format - dd-M-yyyy hh:mm:ss to string is : " + strDate);
15 sdf = new SimpleDateFormat("dd MMMM yyyy");
16 strDate = sdf.format(dt);
17 System.out.println("Date format - dd MMMM yyyy to string is : " + strDate);
18 sdf = new SimpleDateFormat("dd MMMM yyyy zzzz");
19 strDate = sdf.format(dt);
20 System.out.println("Date format - dd MMMM yyyy zzzz to string is : " + strDate);
21 sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
22 strDate = sdf.format(dt);
23 System.out.println("Date format - E, dd MMM yyyy HH:mm:ss z to string is : " + strDate);
24 }
25}
1Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2String s = formatter.format(date);