简体   繁体   English

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

[英]Date format in the spring boot repository testing

I am using the following codes to do the integration testing for repository functions.我正在使用以下代码对存储库功能进行集成测试。 However, it will fail because of the date formart.但是,由于日期格式,它会失败。 The output from SimpleDateFormat is 'Wed Jan 01 10:10:10 MST 2020', but the date from database is '2020-01-01 10:10:10.0'.来自 SimpleDateFormat 的 output 是“Wed Jan 01 10:10:10 MST 2020”,但数据库中的日期是“2020-01-01 10:10:10.0”。 Is their any easy way to handle this issue?他们有什么简单的方法来处理这个问题吗?

      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);

This is how I defined modified date(java.util.date):这就是我定义修改日期(java.util.date)的方式:

@Column(name = "MODIFIED_DATE_TIME") 
@NotNull 
@Temporal(TemporalType.TIMESTAMP) 
private Date modifiedDate;

It would seem to me that TestBean is declared to hold a java.util.Date , but the TestBean instance returned from findById() instead holds a java.sql.Timestamp .在我看来, TestBean被声明为持有java.util.Date ,但从findById()返回的TestBean实例却持有java.sql.Timestamp It's very unfortunate (if it's true).这是非常不幸的(如果这是真的)。

Timestamp is implemented as a subclass of Date . Timestamp作为Date的子类实现。 Both classes are poorly designed and long outdated, we should not use them anymore.这两个类的设计都很糟糕而且已经过时了,我们不应该再使用它们了。 In spite of the subclass relationship, accordindg to the documentation we should not regard Timestamp as a kind of Date .尽管存在子类关系,但根据文档,我们不应将Timestamp视为一种Date Implementing Timestamp as a subclass of Date is a true hack.Timestamp实现为Date的子类是一个真正的 hack。 And putting a Timestamp into a Date field in your object is wrong.Timestamp放入 object 的Date字段中是错误的。 I also don't think it was ever the intention that we should use Timestamp in our model beans.我也不认为我们应该在我们的 model bean 中使用Timestamp It was for transferring data to and from SQL database columns with datatype timestamp and timestamp with time zone only.它用于在 SQL 数据库列之间传输数据,数据类型为timestamptimestamp with time zone

So the good solution is: Change your TestBean class to hold a modern date-time object belonging to a class of java.time, the modern Java date and time API. Edit: Since your datatype in MySQL is datetime , the best match in Java is LocalDateTime . So the good solution is: Change your TestBean class to hold a modern date-time object belonging to a class of java.time, the modern Java date and time API. Edit: Since your datatype in MySQL is datetime , the best match in Java是LocalDateTime时间。 A LocalDateTime is a date and time of day without time zone or UTC offset. LocalDateTime是没有时区或 UTC 偏移量的日期和时间。

If you cannot change the type declared in the TestBean class, there are still a couple of possible improvements:如果您无法更改TestBean class 中声明的类型,则仍有一些可能的改进:

  • Make sure the object holds a Date object as it's declared to do, not a Timestamp .确保 object 拥有声明的Date object,而不是Timestamp
  • Add a getter and setter that return and accept an Instant rather than a Date so that your class can better interoperate with code using java.time.添加一个返回并接受Instant而不是Date的 getter 和 setter,以便您的 class 可以更好地与使用 java.time 的代码进行互操作。 The new methods would do the necessary conversion.新方法将进行必要的转换。 Then use the new setter for setting the value in toReturn .然后使用新的 setter 来设置toReturn中的值。

If you can't do any of this either and you're forced to being part of the hack, there are of course ways to set modifiedDate to an old-fashioned Timestamp .如果您也做不到这些并且被迫成为黑客的一部分,那么当然有一些方法可以将modifiedDate设置为老式的Timestamp I suggest:我建议:

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

I have given the time as 17:10:10.我给的时间是 17:10:10。 This time is in UTC (denoted by the Z ).这次是 UTC(用Z表示)。 I assumed that the MST mentioned in your question is North American Mountain Standard Time (not Malaysian Standard Time?), and if so, this time corresponds to the desired 10:10:10 in your time zone.我假设你的问题中提到的 MST 是北美山区标准时间(不是马来西亚标准时间?),如果是这样,这个时间对应于你所在时区所需的 10:10:10。

Links链接

What you are doing is Parsing(String to Date), you need to Format(Date to String).你正在做的是 Parsing(String to Date),你需要 Format(Date to String)。

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);

Here readFormat and writeFormat are same but they can be different.这里readFormatwriteFormat是相同的,但它们可以不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM