How to convert String to Date type

① First create a date formatting class SimpleDateFormat, then obtain the creation time generated by user operation data from the front end, and finally convert the String type to the Date type through the parse() method of the SimpleDateFormat instantiated object.

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

② Directly convert String type to Date type

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


③ Create the current time and convert Date type to String type

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


④ SimpleDateFormat time format

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


Guess you like

Origin blog.csdn.net/qq_44543774/article/details/131655083