88312

Date issue in spring boot data rest

Question:

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 (<a href="https://stackoverflow.com/questions/31822877/jackson-jsonformat-set-date-with-one-day-less" rel="nofollow">ex1</a>, <a href="https://stackoverflow.com/questions/31822877/jackson-jsonformat-set-date-with-one-day-less/45456037#45456037" rel="nofollow">ex2</a>, <a href="https://stackoverflow.com/questions/17655532/json-serializing-date-in-a-custom-format-can-not-construct-instance-of-java-uti?noredirect=1&lq=1" rel="nofollow">ex3</a>), 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 <a href="https://stackoverflow.com/questions/31822877/jackson-jsonformat-set-date-with-one-day-less" rel="nofollow">this suggestion</a>, 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!

<strong>EDIT:</strong> 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)

Answer1:

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 <a href="https://stackoverflow.com/questions/46151633/how-to-make-default-time-zone-apply-in-spring-boot-jackson-date-serialization" rel="nofollow">here</a>.

@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.

Answer2:

Use dependency <a href="https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-hibernate5" rel="nofollow">https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-hibernate5</a>

For example, if you use Gradle

implementation ('com.fasterxml.jackson.datatype:jackson-datatype-hibernate5:2.9.8')

Recommend

  • How to get the Row Headers functionality in ExtJs?
  • Having Multiple Step Files Opens Multiple Browsers
  • Multiple OR in php if() not seeming to respond properly. Tested array values and all. What am I doin
  • How can I set up Lazy Loading with ZF3 (no ServiceLocator pattern from anywhere)
  • ExtJS - SyntaxError: missing ) in parenthetical
  • Font size in ioslides markdown presentation title page
  • Deploy Spring Boot to Wildfly 10
  • How to get submap of LinkedHashMap by element index?
  • how to run pod install in project directory
  • ndarray field names for both row and column?
  • Degree of parallelism in Apache Flink
  • Convert SimpleDateFormat to DateTimeFormatter
  • How to execute git command from build.gradle?
  • Who's creating the files “/private/var/tmp/Untitled-*.uicatalog”?
  • How can I split a CDockablePane?
  • How to modify Azure DevOps release definition variable from a release task?
  • problem with chinese character
  • Updating the input value on button click Jquery
  • Fill polygon with pattern doesn't work with leaflet
  • Obtaining radio button values for use in switch statement in PHP
  • How can I make an image interactive? [closed]
  • PNG file is NOT keeping transparency?
  • Replacing call to setImageResource() with setImageBitmap to reduce hiccup?
  • How to change selected item position to top in Recyclerview?
  • java.lang.NumberFormatException: unable to parse '' as integer one more time
  • What is the correct way to combine two UserControls into one with two states?
  • Using Autofac with AJAX-enabled WCF Service
  • Vue.js 2: Vue cannot find files from /assets folder (v-for)
  • Nested AJAX Calls using .done
  • Misplaced CAGradientLayer on iPhone 6s
  • if you have a DLL creating a bitmap in memory, how to return it to the browser?
  • python socket.sendto
  • opencv deskewing a contour
  • How to redirect into different page by user type in php and mysql
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • How to make 100% div height between header and footer?
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined