将String类型转化为Date类型
- 创建一个SimpleDateFormat
- 使用SimpleDateFormat的parse方法,将yyyy-MM-dd的类型转化成为一个Date类型
DateFormat df = new SimpleDateFormat("yyyy-MM-dd")
String timestr = "2020-10-1";
Date timedate = df.parse(timestr);
System.out.println(timedate.getTime());
运行结果:
将Date类型转化为String类型
- 创建一个DateFormat
- 使用DateFormat将Date转化为String
Date timedate = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String timestr = df.format(timedate);
System.out.println(timestr);
运行结果: