春季启动数据休息中的日期问题 [英] Date issue in spring boot data rest

查看:55
本文介绍了春季启动数据休息中的日期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理日期时遇到了弹簧数据休息的问题.简而言之,就是将日期推迟一天.例如,如果我有 1111-11-11,它会返回给我 1111-11-10.

I'm having a problem with spring data rest when I'm handling dates. In a brief, it is delaying the date in one day. For example, if I have 1111-11-11 it returns to me 1111-11-10.

SO 中有一些相关的帖子(ex1ex2ex3),但他们都没有解决问题.

There are some related post here in SO (ex1, ex2, ex3), but none of them solved the problem.

我有一个具有此 LocalDate 的实体:

I have an entity with this LocalDate:

@Column(nullable=true)
private LocalDate birthDate;

我也有这个仓库:

@RepositoryRestResource(collectionResourceRel = "person", path = "person")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>{

}

当我将 birthDate 保存在数据库中时(我使用的是 MySQL),它会正确保存.但是,当我进行此查询时,例如:

When I save the birthDate in the database (I'm using MySQL), it saves correctly. However, when I make this query, for example:

Person p = personRepo.findById(1L).get();

获取日期晚了一天(就像上面的例子一样).我试图实现这个建议,即改变LocalDate to Date 并包含 Jackson 注释,但它不起作用.我也尝试包含 jackson-modules-java8,但问题仍然存在.

The date is fetched with one day late (just like in the example above). I tried to implement this suggestion, that is, change the LocalDate to Date and include the Jackson annotation, but it doesn't work. I also tried to include the jackson-modules-java8, but the problem still.

现在是最有趣的事情.因为我只是在测试,所以我包括了日期 1111-11-11.我将其更改为今天的日期 2019-02-06.然后,获取工作!这个时候,我想是不是很旧的日期有问题.因此,例如,我尝试了 1970-01-01,而春天又回到了 1969-12-31.我意识到如果我在数据库中包含 1986-01-01 以上的日期,一切正常.但是,如果我包含低于此值的任何内容,我的日期就会晚一天.

Now the most intriguing thing. As I was just testing I was including the date 1111-11-11. I changed that for today's date 2019-02-06. Then, the fetch works! At this time, I think if it was a problem with very old dates. Thus, I tried, for example, 1970-01-01, and the spring returned 1969-12-31. I realized that if I include in the database dates above 1986-01-01 everything works fine. However, if I include anything below that, I got the date one day late.

有人对这个问题有一些暗示吗?

Is someone has some hint about this issue?

感谢您的宝贵时间!

我还检查了我的数据库时区,没问题!

I also checked my database timezone and it's ok!

+--------------------+---------------------+--------------------+
| @@GLOBAL.time_zone | @@session.time_zone | @@system_time_zone |
+--------------------+---------------------+--------------------+
| SYSTEM             | SYSTEM              | -02                |
+--------------------+---------------------+--------------------+
1 row in set (0,00 sec)

推荐答案

终于找到了解决方案.在@ILyaCyclone 和@OleV.V. 之后.评论,我开始搜索时区、spring boot 和 LocalDate.实际上 LocalDate 不携带 UTC 信息.但是,当我从数据库中获取这些数据时,JVM 需要进行转换以使 SQL date 成为 LocalDate.

Finally, I found a solution. After the @ILyaCyclone and @OleV.V. comments, I started to search about timezone, spring boot, and LocalDate. Indeed the LocalDate doesn't carry UTC information. However, when I fetch this data from the database, the JVM needs to make a conversion to make the SQL date become the LocalDate.

因此,我做的第一件事就是检查数据库时区:

Therefore, the first thing I made was to check the database timezone:

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) as GMT_TIME_DIFF;

返回:

+--------------------+---------------------+--------------------+
| @@GLOBAL.time_zone | @@session.time_zone | @@system_time_zone |
+--------------------+---------------------+--------------------+
| SYSTEM             | SYSTEM              | -02                |
+--------------------+---------------------+--------------------+

和:

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP)

返回:

+--------------------------------+
| TIMEDIFF(NOW(), UTC_TIMESTAMP) |
+--------------------------------+
| -02:00:00                      |
+--------------------------------+

所有这些 SQL 答案都没有问题.所以,问题出在弹簧靴上.解决问题的是在代码中设置UTC.我在这里找到了这个提示.

All these SQL answers were ok. So, the problem was in the spring boot. What solved the problem was to set the UTC in the code. I found this hint here.

@PostConstruct
void started() {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

之后,所有日期都开始正常工作.

After that, all the dates started to work properly.

问题解决了.但是,我对1986年之后和之前日期的问题一无所知.如果有人有任何提示,请与我分享.

Is solved the problem. However, I have no clue about the issue regarding the date after and before 1986. If someone has some hint, please share with me.

感谢你们付出了一些努力来帮助我.我真的很感激.

Thank you guys who put some effort to help me. I really appreciate that.

这篇关于春季启动数据休息中的日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆