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

标签 mysql jsp

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

最佳答案

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
}   

关于mysql - 使用具有不同日期格式的jsp将日期字段插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39032973/

相关文章:

java - jsp - 未正确使用 JSTL 的错误

java - 如何检查html文本框中的空值?

内爆计数大于 2 的数组时出现 PHP INSERT 错误

mysql - SELECT AND INSERT QUERY into multiple table with conditions

mysql - 如何使用 Propel ORM 在 Symfony 中构建自引用表

spring - JASIG CAS : single sign out not working

html - 将文本与复选框对齐

mysql - 使用 IPV6 连接到 Google Cloud SQL

php - 使用PHP进行拇指扫描设备

java - 将对象数组从 servlet 发送到 JSP