web页面的时间传入servlet如何转换为可以存入MySQL的Date类型

在web页面中当使用如下语句:

<input type="date" name="startTime"/>

提交到servlet中

在servlet页面中:

String startTime = request.getParameter("startTime");

获取到的是字符串类型的时间日期,如下:

2019-06-30

因此需要将字符串类型的时间日期2019-06-03转换为MySQL可以识别的类型存入,如下:

Date startDate=null;

// 注意:SimpleDateFormat构造函数的样式与startTime的样式必须相符,如果不知道的话,可以在servlet页面中输出一下,看一下获取到的时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//使用自己的时间格式
// 必须捕获异常

try {
     startDate = simpleDateFormat.parse(startTime);
  } catch (ParseException px) {
     px.printStackTrace();
  }

startDate即为可以直接存入MySQL的数据类型

原文地址:https://www.cnblogs.com/mhm111/p/11110040.html