我使用Spring编写了一个REST服务。REST服务正在PaaS上运行。我有一个端点可以在DB表中创建新项。每次创建新项时,我都会使用以下方法为其分配创建日期:
LocalDate currentDate = LocalDate.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
sql = "INSERT INTO my_table (" +
"code, " +
"description, " +
"created_date)" +
"VALUES (?, ?, ?)";
jdbcTemplate.update(
sql,
new Object[]{
code,
description,
currentDate.format(df)
}
);问题是,currentDate.format(df)将于2017年10月17日发布,而今天是2017年10月24日。我在10月17日看到的唯一连接是,这是我最后一次在PaaS上重新启动PaaS文件。我已经进入了运行服务并运行SSH的环境中。它返回Tue Oct 24 17:54:23 UTC 2017。知道我做错什么了吗?
发布于 2017-10-24 18:34:44
从您的意思来看,我猜您的问题是在程序开始时初始化了currentDate,然后继续保存同一日期,您可能有这样的情况
LocalDate currentDate = LocalDate.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public void insert(String code, String description) {
sql = "INSERT INTO my_table (" +
"code, " +
"description, " +
"created_date)" +
"VALUES (?, ?, ?)";
jdbcTemplate.update(
sql,
new Object[]{
code,
description,
currentDate.format(df)
}
);
}应该在方法中移动日期的实例化。
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public void insert(String code, String description) {
LocalDate currentDate = LocalDate.now();
sql = "INSERT INTO my_table (" +
"code, " +
"description, " +
"created_date)" +
"VALUES (?, ?, ?)";
jdbcTemplate.update(
sql,
new Object[]{
code,
description,
currentDate.format(df)
}
);
}https://stackoverflow.com/questions/46917561
复制相似问题