序言
1、Date 转 String
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
System.out.println(dateString);
2、String转Date
String dateStrings="2019-10-11 10:10:10";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date parse = format.parse(dateStrings);
System.out.println(parse);
}catch (Exception e){
e.printStackTrace();
}
时间戳转换Date
SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long dateTime = System.currentTimeMillis();
String c =formats.format(dateTime);
try {
System.out.println(formats.parse(c));
}catch (Exception e){
e.printStackTrace();
}