Use java.time package of Java8 instead of former java.util.Date solution - alexpron/shanoir-ng GitHub Wiki
Date and time part is used everywhere in our project. It's important to understand it and well use it to avoid known bugs found during the use of former java.util.Date package.
A new package java.time came out since the release of Java 8. This package contains classes as LocalDate, LocalTime, LocalDateTime, ZonedDateTime and Instance which are aiming to replace the former package java.util.Date. Please find the javadoc of the new package here.
Here is a table that shows quickly the differences between each solution, in DB, in Java classes, in Json sent between the front and the back end and in Javascript which is used to display the Date in the front end.
| date | date + time + tz | date + time | former solution | |
|---|---|---|---|---|
| MYSQL | DATE | TIMESTAMP | TIMESTAMP | |
| JAVA | LocalDate | ZonedDateTime | LocalDateTime | |
| Json | yyyy-MM-dd | yyy-MM-ddT00:00:00+01:00 | yyy-MM-ddT00:00:00 | |
| Javascript | Date | Date | Date |
- Date is sent in the json files as an instant in time (timestamp), which is not readable. The timestamp doesn't have a time zone, neither a format or a calendar system.
- Many bugs and problems have been reported by java developer about the former package.
- In the context of shanoir, LocalDate class could be enough in most of the situations. We do not need to save the timezone and we could avoid bugs coming from bad calculation of timezone.
- In the future, ZonedDateTime class could be a good choix for saving information like date and time of import. Timezone configuration should be done in the server side to save the zoned date and time as a timestamp in the DB. Timezone could be one of the user's preferences to be selected in the front.
The shanoir-ng project is based on spring boot 1.4.1(java 8, jpa 2.1). Since the jpa 2.1 came out before the java 8, we should use the support of java8 in hibernate5 as configured in the pom.xml of shanoir-ng-ms-common:
<!-- DateTime -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
This hibernate specific support of java 8 avoid development of custom codes to convert java 8 classes into hibernate supported ones. You can find examples of implementation in MS datasets or import with the jackson annotations.
To avoid the repetition work dealing with Date, LocalDate, conversion, format, etc, I created several methodes and annotations to be reused in the projet. There is what could interest you in the org.shanoir.ng.shared.dateTime package:
- One annotation @LocalDateAnnotations which groups the LocalDate Serializer, Deserialize and format annotations.
- Conversions between Date and LocalDate, LocalDateTime, etc..
- Formatter to deal with pacs data with yyyyMMdd format
When we migrate the spring boot version to 2.0+ (java8 and jpa2.2), we could no longer use the jsr310 support as described before. It will contain support for LocalDate, LocalDateTime classes for the moment, but not for ZonedDateTime class yet. If we would like to persiste a ZonedDateTime object in BD, hibernate will convert the object to local timezone of your JVM and store it as a timestamp without time zone information. For example, an import is done at 2019-01-17 00:00:00 in France (time zone UTC +1 since winter). In the DB, the information will be stored as the timestamp of 2019-01-16 23:00:00 with the UTC time zone. When we try to get this information from the server, it's important that the time zone of the server is well configured to get the 2019-01-17 00:00:00 as real import data time. This configuration could be done in the hibernate.jdbc.time_zone parameter. Be carefully that all instance of shanoir-ng in France use the same time zone when we introduce the ZonedDateTime in the future.
Source: https://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate