【问题标题】:JPA Saving wrong date in MySQL databaseJPA 在 MySQL 数据库中保存错误的日期
【发布时间】:2017-06-15 19:01:43
【问题描述】:

我的 MySQL 数据库中有一个包含日期列的表:

+-------------------+---------------+------+-----+---------+----------------+
| Field             | Type          | Null | Key | Default | Extra          |
+-------------------+---------------+------+-----+---------+----------------+
| id                | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| type              | varchar(50)   | NO   |     | NULL    |                |
| expiration        | date          | NO   |     | NULL    |                |

我正在使用带有 JPA 的 MySQL 来保存日期。我有一个功能,用户可以选择最终日期范围,它将获取所有日期。

查看这段代码(带有一堆 SYSO),看看发生了什么......

@Override
    protected DateTime nextReference(DateTime reference) {
        System.out.println("Reference: " + reference.toString("dd-MM-YYYY"));
        DateTime plus = reference.plusMonths(1);
        System.out.println("One month from now: " + plus.toString("dd-MM-YYYY"));

        DateTime result = plus.withDayOfMonth(reference.getDayOfMonth());
        System.out.println("Final: " + result.toString("dd-MM-YYYY"));

        return result;
    }

这部分,结果还不错:

Reference: 10-01-2017
One month from now: 10-02-2017
Final: 10-02-2017
Reference: 10-02-2017
One month from now: 10-03-2017
Final: 10-03-2017
Reference: 10-03-2017
One month from now: 10-04-2017
Final: 10-04-2017
Reference: 10-04-2017
One month from now: 10-05-2017
Final: 10-05-2017
Reference: 10-05-2017
One month from now: 10-06-2017
Final: 10-06-2017
Reference: 10-06-2017
One month from now: 10-07-2017
Final: 10-07-2017
Reference: 10-07-2017
One month from now: 10-08-2017
Final: 10-08-2017
Reference: 10-08-2017
One month from now: 10-09-2017
Final: 10-09-2017
Reference: 10-09-2017
One month from now: 10-10-2017
Final: 10-10-2017
Reference: 10-10-2017
One month from now: 10-11-2017
Final: 10-11-2017
Reference: 10-11-2017
One month from now: 10-12-2017
Final: 10-12-2017
Reference: 10-12-2017
One month from now: 10-01-2018
Final: 10-01-2018

好的,现在让我们进入保存部分:

@Transactional
private void saveTransactions(List<Transaction> transactions) {
    for (Transaction t : transactions) {
        System.out.println("Saving: " + t.getExpiration().toString("dd-MM-YYYY"));
        Transaction saved = dao.save(t);
        System.out.println("Saved: " + saved.getExpiration().toString("dd-MM-YYYY"));
    }
}

如您所见,我还添加了一些行来调试它......在继续输出之前,请检查 DAO:

public T save(T entity) {
    entityManager.persist(entity);
    return entity;
}

没什么大不了的...输出:

Saving: 10-02-2017
Saved: 10-02-2017
Saving: 10-03-2017
Saved: 10-03-2017
Saving: 10-04-2017
Saved: 10-04-2017
Saving: 10-05-2017
Saved: 10-05-2017
Saving: 10-06-2017
Saved: 10-06-2017
Saving: 10-07-2017
Saved: 10-07-2017
Saving: 10-08-2017
Saved: 10-08-2017
Saving: 10-09-2017
Saved: 10-09-2017
Saving: 10-10-2017
Saved: 10-10-2017
Saving: 10-11-2017
Saved: 10-11-2017
Saving: 10-12-2017
Saved: 10-12-2017

如您所见...应该没问题吧?一切都在 10 号。

在我再次继续之前,请检查模型和转换器:

    //Attribute
    @Convert(converter = JpaDateConverter.class)
    private DateTime expiration;

//Converter

public class JpaDateConverter implements AttributeConverter<DateTime, Date> {

    @Override
    public Date convertToDatabaseColumn(DateTime objectValue) {
        return objectValue == null ? null : new Date(objectValue.getMillis());
    }

    @Override
    public DateTime convertToEntityAttribute(Date dataValue) {
        return dataValue == null ? null : new DateTime(dataValue);
    }

}

现在看看我的数据库:

mysql> select expiration from tb_transaction where notes = 3 and year(expiration
) = 2017;
+------------+
| expiration |
+------------+
| 2017-01-10 |
| 2017-02-10 |
| 2017-03-09 |
| 2017-04-09 |
| 2017-05-09 |
| 2017-06-09 |
| 2017-07-09 |
| 2017-08-09 |
| 2017-09-09 |
| 2017-10-09 |
| 2017-11-10 |
| 2017-12-10 |
+------------+
12 rows in set (0.00 sec)

出于一些奇怪而神秘的原因,有些日期保存在 9 号而不是 10 号!!

没有警告,MySQL 驱动程序没有错误或什么都没有。

请帮助大家!

EDIT事务类:

@Entity
@Table(name = "tb_transaction")
public class Transaction implements Cloneable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(STRING)
    private TransactionType type;

    @Convert(converter = JpaDateConverter.class)
    private DateTime expiration;

【问题讨论】:

  • 使用日期导入添加您的事务映射类
  • 可能是因为夏令时。 DST 发生在 3 月和 11 月,请注意您的日期在这些时间发生了变化。
  • 瑞克你知道任何解决方法吗?
  • @MaciejKowalski 我更新了这个问题。你是这个意思吗?
  • 在您的转换器类中,必须有一种方法可以将DateTime 转换为Date,只需采用DateTimeDate 部分并忽略Time。调用 getMillis() 会导致 DST 问题。

标签: java mysql date jpa jodatime


【解决方案1】:

tl;博士

使用JPA 2.2来支持java.time

在 Java 中使用仅日期类来处理 SQL 中的仅日期值。

LocalDate                           // Represent a date-only, without a time-of-day and without a time zone.
.now(                               // Get today's date…
    ZoneId.of( "Africa/Tunis" )     // …as seen in the wall-clock time used by the people of a particular region.
)                                   // Returns a `LocalDate` object.
.plusMonths( 1 )                    // Returns another `LocalDate` object, per immutable objects pattern.

java.time

JPA 2.2 现在支持现代的 java.time 类。不再需要使用 Joda-Time。

不要使用java.sql.Date该类假装表示仅日期,但实际上设置了时间到 UTC,因为从 java.util.Date 继承的糟糕设计决定(尽管名称代表日期时间UTC的偏移量为零本身)。这些遗留类是一个糟糕透顶的烂摊子。随着 JSR 310 的采用,Sun、Oracle 和 JCP 社区几年前都放弃了这些课程,您也应该如此。

LocalDate

LocalDate 类表示仅日期值,没有时间,也没有 time zoneoffset-from-UTC

时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜过后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

如果没有指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能在运行时(!)期间change at any moment,因此您的结果可能会有所不同。最好将您想要/预期的时区明确指定为参数。如果关键,请与您的用户确认该区域。

Continent/Region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 2-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

如果你想使用 JVM 当前的默认时区,请求它并作为参数传递。如果省略,代码会变得难以阅读,因为我们不确定您是否打算使用默认值,或者您是否像许多程序员一样没有意识到这个问题。

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

或者指定一个日期。您可以通过数字设置月份,1 月至 12 月的编号为 1-12。

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

或者,更好的是,使用预定义的Month 枚举对象,一年中的每个月一个。提示:在整个代码库中使用这些 Month 对象,而不是仅仅使用整数,以使您的代码更具自我记录性、确保有效值并提供 type-safetyYearYearMonth 同上。

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

日期时间数学

显然你想从一个日期开始,然后一个月后作为日期范围。

LocalDate monthLater = ld.plusMonths( 1 ) ;

JDBC 4.2

从 JDBC 4.2 开始,您的 JDBC 驱动程序需要支持一些关键的 java.time 类,例如 LocalDate

日期范围

请注意以下 ThreeTen-Extra 的链接。如果您对日期范围做了很多工作,您可能会发现 LocalDateRange 类会很方便。


关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。

从哪里获得 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

【讨论】:

    【解决方案2】:

    感谢@RickS,我们解决了这个问题。

    PS:我很久以前就解决了这个问题,2年后再次遇到这个问题。哈哈

    解决方案在转换器中:JpaDateConverter,当您将 DateTime Objetivo 转换为 java.sql.Date 时,根据文档:

    如果给定的毫秒值包含时间信息,驱动程序 将时间组件设置为默认时区中的时间( 运行应用程序的 Java 虚拟机的时区) 对应于 0 GMT。

    为了解决这个问题,我更改了服务器时区:

    dpkg-reconfigure tzdata
    

    在 MySQL 中:

    SET @@global.time_zone = '+00:00';
    

    【讨论】:

    • 这个答案对我有用,我正在插入一个日期(例如LocalDate.of(1961, 3, 15))并在 MySQL 数据库中获取前一天(例如“1961-03-14”)。使用SET 命令为我解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2014-11-19
    相关资源
    最近更新 更多