使用具有不同日期格式的jsp将日期字段插入数据库

时间:2016-08-19 07:04:14

标签: mysql jsp

我正在尝试将日期字段保存到mysql表中。 我的jsp表单中的日期字段格式为MM / DD / YYY,数据库格式为YYYY / MM / DD。 如何从表单中检索日期并以YYYY / MM / DD格式插入表格?

1 个答案:

答案 0 :(得分:0)

String publishdt = request.getParameter("publish");
if(publishdt == null)
{   
    publishdt = "";
}
else
{
    try 
        {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
            LocalDate date = LocalDate.parse(publishdt, formatter);
            publish = String.format("%s", date);
        }
    catch (DateTimeParseException exc) 
        {
            out.println("%s is not parsable!%n"+publish);
            throw exc;      // Rethrow the exception.
        }
        // 'date' has been successfully parsed
}