import java.text.Format;
2 import java.text.ParseException;
3 import java.text.SimpleDateFormat;
4 import java.util.Date;
5
6 public class DateFormatToString {
7 protected static Format format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
8
9 public static void main(String[] args) {
10
11 Date date = new Date();
12 System.out.println(format.format(date));
13
14 try {
15 Date date1 = (Date) format.parseObject("2005-11-15 20:55:48");
16 System.out.println(date1);
17 } catch (ParseException e) {
18 // TODO Auto-generated catch block
19
20 }
21
22 format = new SimpleDateFormat("yyyy-MM-dd");
23 System.out.println(format.format(date));
24 format = new SimpleDateFormat("hh:mm:ss");
25 System.out.println(format.format(new Date()));
26
27 }
28 }
输出
1
2005-11-15 09:02:30
2
Tue Nov 15 20:55:48 CST 2005
3
2005-11-15
4
09:02:30