Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date issue in spring boot data rest

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.

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

I have an entity with this LocalDate:

@Column(nullable=true)
private LocalDate birthDate;

I also have this repository:

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

}

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

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.

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?

Thank you for your time!

EDIT: 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)
like image 669
André Pacheco Avatar asked Feb 06 '19 20:02

André Pacheco


People also ask

How do you pass a date in rest?

Want to pass date /time parameter in a REST message We need to pass a parameter called "CHANGEDATE" while sending the GET method . " Change date for list of Configuration Items to be returned (for example records modified in last 3 days). If value is empty, all Configuration Item records are returned. "

How do I extract data between two dates in spring boot?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

Why is Spring Data rest not recommended?

Real-world applications should avoid using Spring Data REST because the entities are exposed as RESTful Services. The two most critical considerations in designing a RESTful service are the domain model and the consumers.


1 Answers

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;

which returned:

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

and:

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP)

returning:

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

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.

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.

like image 53
André Pacheco Avatar answered Sep 30 '22 14:09

André Pacheco