Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring启动存储库测试中的日期格式_Java_Spring Boot_Integration Testing_Simpledateformat_Assertj - Fatal编程技术网

Java spring启动存储库测试中的日期格式

Java spring启动存储库测试中的日期格式,java,spring-boot,integration-testing,simpledateformat,assertj,Java,Spring Boot,Integration Testing,Simpledateformat,Assertj,我正在使用以下代码对存储库功能进行集成测试。但是,由于启动日期的原因,它将失败。SimpleDataFormat的输出为“Wed Jan 01 10:10:10 MST 2020”,但数据库的日期为“2020-01-01 10:10:10.0”。他们有没有简单的方法来处理这个问题 Optional<TestBean> testBean = testBeanRepository.findById(1L); TestBean toReturn = new Test

我正在使用以下代码对存储库功能进行集成测试。但是,由于启动日期的原因,它将失败。SimpleDataFormat的输出为“Wed Jan 01 10:10:10 MST 2020”,但数据库的日期为“2020-01-01 10:10:10.0”。他们有没有简单的方法来处理这个问题

      Optional<TestBean> testBean = testBeanRepository.findById(1L);
      TestBean toReturn = new TestBean();
      toReturn.setAccountNumber(123456789);
      toReturn.setId(1L);
      toReturn.setCustomerName("Peter");
      toReturn.setModifiedDate(new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").parse("2020-01-01 10:10:10"));
      assertThat(testBean)
         .isPresent()
         .hasValue(toReturn);

您所做的是解析(从字符串到日期),您需要格式化(从日期到字符串)

SimpleDateFormat readFormat=新的SimpleDateFormat(“yyyy-mm-dd-HH:mm:ss”);
SimpleDateFormat writeFormat=新的SimpleDateFormat(“yyyy-mm-dd HH:mm:ss”);
可选testBean=testBeanRepository.findById(1L);
TestBean toReturn=newtestbean();
toReturn.setAccountNumber(123456789);
toReturn.setId(1L);
toReturn.setCustomerName(“彼得”);
setModifiedDate(writeFormat.format(readFormat.parse(“2020-01-01 10:10:10”));
assertThat(testBean)
.isPresent()
.hasValue(toReturn);

这里的
readFormat
writeFormat
是相同的,但它们可以不同。

在我看来,
TestBean
被声明为持有
java.util.Date
,但是从
findById()
返回的
TestBean
实例却持有
java.sql.Timestamp
。这很不幸(如果是真的话)

Timestamp
作为
Date
的子类实现。这两个类的设计都很糟糕,而且早已过时,我们不应该再使用它们了。尽管存在子类关系,但根据文档,我们不应将
时间戳
视为一种
日期
。将
Timestamp
实现为
Date
的子类是一个真正的黑客行为。将
时间戳
放入对象中的
日期
字段是错误的。我也不认为我们应该在模型bean中使用
Timestamp
。它仅用于将数据类型为
timestamp
timestamp且带有时区的SQL数据库列之间的数据传输

因此,好的解决方案是:更改您的
TestBean
类,以保存属于java.time类(现代java日期和时间API)的现代日期时间对象。编辑:因为MySQL中的数据类型是
datetime
,所以Java中的最佳匹配是
LocalDateTime
LocalDateTime
是一天中没有时区或UTC偏移的日期和时间

如果您无法更改
TestBean
类中声明的类型,那么仍有一些可能的改进:

  • 确保该对象按声明的方式保存
    日期
    对象,而不是
    时间戳
  • 添加一个getter和setter,返回并接受
    即时
    而不是
    日期
    ,这样类就可以更好地使用java.time与代码进行互操作。新方法将进行必要的转换。然后使用新的设置器在
    返回中设置值
如果你也做不到这一点,并且被迫成为黑客的一部分,那么当然有办法将
modifiedDate
设置为老式的
时间戳。我建议:

    toReturn.setModifiedDate(Timestamp.from(Instant.parse("2020-01-01T17:10:10Z")));
我给的时间是17:10:10。此时间以UTC为单位(由
Z
表示)。我假设您的问题中提到的MST是北美山区标准时间(不是马来西亚标准时间?),如果是这样,那么这个时间对应于您所在时区所需的10:10:10

链接
  • 解释如何使用java.time

是否更改格式以匹配数据库?您能否详细说明您是如何尝试解决此问题的,您的代码不清楚您的尝试在哪里。我建议您不要使用
SimpleDateFormat
Date
。这些类设计得很糟糕,而且早已过时,其中前者尤其令人讨厌。例如使用
LocalDateTime
DateTimeFormatter
,两者都来自.Detail,我更喜欢
assertThat(testBean).isEqualTo(可选的.of(toReturn))
。对我来说似乎更简单,无论结果是否包含错误值、是否为空或甚至为空,都会给您一条非常好的错误消息。
setModifiedDate()
声明为接受的类型是什么<代码>java.util.Date
?你能把它换成现代的吗<代码>即时
可能是合适的,具体取决于具体的要求。@OleV.V。对我使用的是
java.util.Date
。和
@Column(name=“MODIFIED_DATE_TIME”)@NotNull@Temporal(TemporalType.TIMESTAMP)private DATE modifiedDate谢谢你的回复。我正在使用java.util.date,并且在字段声明中使用了时态。我应该如何改变它?我已经更新了我的问题。这取决于您的JPA实现和版本、数据库引擎以及数据库中
MODIFIED\u DATE\u TIME
列的数据类型。也许您可以简单地声明字段,例如
OffsetDateTime
Instant
(那么您是否还需要
@Temporal
注释,我不知道)。我使用的是MySQL,日期的数据类型是DATETIME。然后在Java中尝试
LocalDateTime
,它是MySQL
datetime
的最佳匹配项。modifiedDate的类型为java.util.Date。我已经更新了我的问题。
SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");

Optional<TestBean> testBean = testBeanRepository.findById(1L);
TestBean toReturn = new TestBean();
toReturn.setAccountNumber(123456789);
toReturn.setId(1L);
toReturn.setCustomerName("Peter");
toReturn.setModifiedDate(writeFormat.format(readFormat.parse("2020-01-01 10:10:10")));
assertThat(testBean)
        .isPresent()
        .hasValue(toReturn);

    toReturn.setModifiedDate(Timestamp.from(Instant.parse("2020-01-01T17:10:10Z")));