问题:请求中的时间应该用什么类型?对应的controller中应该做什么处理?service中的时间类型是不是只能是Date?
解决1:请求中的类型为2018-03-22 15:34:00 service中时间参数为Date类型,controller中怎么处理?
将请求中的事件类型定义为String,然后再controller中做解决2中的操作,执行查询时传对应的Date参数。解决!
解决2:前端时间控件传到后台的请求时间类型为String,service中时间参数为Date类型,controller中怎么处理?
Date startDate = null;
String startDateString = request.getParameter("startDate");
if (StringUtils.isNotBlank(startDateString)) {
try {
startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDateString);
} catch (ParseException e) {
}
}
Date endDate = null;
String endDateString = request.getParameter("endDate");
if (StringUtils.isNotBlank(endDateString)) {
try {
endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDateString);
} catch (ParseException e) {
}
}
log.info("startDate: {} endDate: {}", startDate, endDate);
核心语句:
startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDateString);
将String类型的时间转换成标准
"yyyy-MM-dd HH:mm:ss"的Date类型,目标实现。