LocalDate.now()返回错误的日期

时间:2017-10-24 18:26:20

标签: java spring-boot localdate

我使用Spring Boot编写了一个REST服务。 REST服务正在PaaS上运行。我有一个端点在数据库表中创建新项目。每次创建新项目时,我都会使用以下命令为其指定创建日期:

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上的{1}}文件。我JAR进入运行服务的环境并运行SSH。它返回date。我有什么想法吗?

1 个答案:

答案 0 :(得分:4)

根据你所说的我猜你的问题是你的节目在你的节目开始时初始化,然后你继续保存相同的日期,你可能有类似的东西

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)
                }
        );
}
相关问题