LocalDate.now() returns 日期错误

LocalDate.now() returns wrong date

我已经使用 Spring Boot 编写了一个 REST 服务。 REST 服务 运行 在 PaaS 上运行。我有一个端点可以在数据库 table 中创建新项目。每次创建新项目时,我都会使用以下方式为其分配创建日期:

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 上的 JAR 文件。我已经 SSH 进入服务 运行ning 和 运行 date 的环境。它returnsTue Oct 24 17:54:23 UTC 2017。知道我做错了什么吗?

根据你所说的,我猜你的问题是 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)
                }
        );
}