本文介绍了转换LocalDate.MAX为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Spring Data JPA访问Derby-DB.时间值定义为java.time.LocalDate(java 8时间api).现在,Spring Data附带了org.springframework.data.convert.Jsr310Converters.LocalDateToDateConverter,可将LocalDate转换为java.util.Date,因为据我所知,JPA规范到目前为止不支持LocalDate.

We are using Spring Data JPA to access a Derby-DB. Temporal values are defined as java.time.LocalDate (java 8 time api). Spring Data now is shipped with a org.springframework.data.convert.Jsr310Converters.LocalDateToDateConverter to convert LocalDate to java.util.Date, since - far as I know - JPA specification does not support LocalDate so far.

现在将LocalDate.MAX转换为Date时出现问题,导致以下异常(已剥离):

We now have an issue when converting a LocalDate.MAX to Date, resulting in the following exception (stripped):

Caused by: java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow
at java.util.Date.from(Unknown Source)
at org.springframework.data.convert.Jsr310Converters$LocalDateToDateConverter.convert(Jsr310Converters.java:116)
at org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters$LocalDateConverter.convertToDatabaseColumn(Jsr310JpaConverters.java:52)
at org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters$LocalDateConverter.convertToDatabaseColumn(Jsr310JpaConverters.java:47)
at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$1.bind(AttributeConverterSqlTypeDescriptorAdapter.java:97)
... 79 more
Caused by: java.lang.ArithmeticException: long overflow
at java.lang.Math.multiplyExact(Unknown Source)
at java.time.Instant.toEpochMilli(Unknown Source)
... 84 more

还有谁遇到过这个问题并提出了解决这个问题的建议吗?

Anyone who also faced this issue and have a suggestion to overcome this issue?

推荐答案

java.util.Date不支持将来的日期.参见其 javadoc :

java.util.Date doesn't support dates that far into the future. See its javadoc :

因此,您将必须定义自己的JPA AttributeConverter,并且可能将其存储为String或其他类型

Consequently you would have to define your own JPA AttributeConverter and maybe store it as a String, or some other type

这篇关于转换LocalDate.MAX为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 00:48