不可避免的,我们会遇到一些日期与字符串相互转换的情况.以下,给出简单的操作:
- 从字符串转Date用的是parse方法
String s = "2018-09-26 18:44:23";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(s);
- 从Date转换成字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = sdf.format(date);