如何将String转Date类型

① 先创建一个日期格式化类 SimpleDateFormat ,然后从前端获取用户操作数据产生的创建时间,最后通过SimpleDateFormat 实例化对象的parse()方法将String类型转换成Date类型。

//String转Date
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = request.getParameter("CreateTime");
        Date date = simpleDateFormat.parse(createTime);
             

② 直接将String类型转Date类型

//String转Date
Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-11-01 08:30:20");


③ 创建现在时间,将Date类型转换为String类型

//Date转String
Date date2 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date2);


④ SimpleDateFormat 时间格式

年yyyy
月MM
日dd
时HH
分mm
秒ss
毫秒SS


猜你喜欢

转载自blog.csdn.net/qq_44543774/article/details/131655083