I'm using Spring Boot 1.5.4, Hibernate 5.2.10, Spring Data REST, HATEOAS, JDK8 with LocalDate and LocalDateTime.
My computer is on CEST timezone but I want the application works in UTC, so I set in application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/database?useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=true
spring.jpa.hibernate.jdbc.time_zone = UTC
According to this article I don't want to change the timezone of my JVM because seems not to be a best practice.
Spring Data REST exposes my repositories and I use Swagger2 to have a nice interface to use API.
When I try a endpoint I see something like:
{
"_embedded": {
"dailyCodes": [
{
"sid": "d495cdaa-14f2-44cb-a98f-8aa6ddd43d91",
"createdDate": "2017-06-28T16:20:01",
"lastModifiedDate": "2017-06-28T16:20:01",
"lastModifiedBy": "admin",
"date": "2017-06-28",
"code": "js",
"new": false,
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/dailyCodes/1"
},
"dailyCode": {
"href": "http://localhost:8080/api/v1/dailyCodes/1"
}
}
}
Like you can see the datetime format is fine and also it display the CEST time even if in the db the real time is 14:20:01.
I guess this is wrong because my REST API should work in UTC.
How could I achieve this result?
Always on the same topic, I've a REST endpoint (exposed by Spring Data REST) to search using LocalDate params; I'm using
@Transactional
@PreAuthorize("isAuthenticated()")
public interface DailyCodeRepository extends PagingAndSortingRepository<DailyCode, Long> {
@Query("SELECT d FROM DailyCode d WHERE (:code IS NULL or code=:code) AND (:from IS NULL OR date>=:from) AND (:to IS NULL OR date<=:to)")
public Page<DailyCode> findAllWithParameter(@Param("code") @RequestParam(value = "code", required = false) String code,
@Param("from") @RequestParam(value = "from", required = false) LocalDate from,
@Param("to") @RequestParam(value = "to", required = false) LocalDate to, Pageable pageable);
}
Also in this case I've a strange thing: if I call the endpoint passing parameters, in the server they arrive with 1 day less. Furthemore the date pattern accepted seems to be the JDK default of my locale (Italian) but I guess it's not a best practice.
Is there a best practice to follow to avoid any problems with date/time arguments in both direction?
See Question&Answers more detail:
os