String类型与Date类型之间的相互转化
业务说明
在一些表单数据提交的过程中,我们经常会遇到日期类型的数据,比如:生日、创建时间等。那么String类型与Date类型之间是如何转换的呢?
String类型转Date类型
在这里我们需要导入类,将字符串类型的“yyyy-MM-dd”的格式转化为Date类型
就需要调用parse()方法
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birthDate = new Date();
try {
birthDate = (birth);
} catch (ParseException e) {
();
}
String类型已经成功转换为Date类型了,注意一点,使用SimpleDateFormat需要捕获ParseException异常
Date类型转为String类型
这里需要使用的方法为format()方法
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birthDate=new Date();
String s = (birthDate);