Date --> String
使用 public final String format(Date date) 方法
代码如下:
Date d
= Date();
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str
= sdf
.format(d
);
System
.out
.println(str
);
String --> Date
使用 public Date parse(String source) 方法 代码如下:
String s
= "2019-05-22 11:25:55";
SimpleDateFormat sdf2
= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date d2
= sdf2
.parse(s
);
System
.out
.println(d2
);