0

Can you use LocalDateTime with a Spring Boot project and if so how?

I tried to follow this post and added the dependancy and the line required in application.properties but I still get :

java.io.StreamCorruptedException: invalid stream header: 32303137

When persisting data or trying to view existing data with dates created using Java.Util.Date.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Al Grant
  • 2,102
  • 1
  • 26
  • 49
  • You can use `java.time` types with any JPA provider that supports those types, and since those types are standard in JPA 2.2 then any compliant JPA 2.2 provider –  Oct 10 '17 at 06:18
  • Spring-boot is an opinionated framework. If you know how to get Springboot onto JPA2.2 please let me know. – Al Grant Oct 10 '17 at 06:30
  • Try removing from @Temporal(TemporalType.TIMESTAMP) from your entity fields. – Justinas Jakavonis Oct 10 '17 at 07:49

1 Answers1

1

Ok, so I got it to go. It required multiple changes to make both Hibernate & Springboot & Thymeleaf all work with Java 8 - LocalDateTime.

Hibernate

Add dependencies:

compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.4.0")
compile group: 'org.hibernate', name: 'hibernate-java8'

Add the following to application.properties:

spring.jackson.serialization.write_dates_as_timestamps=false

The annotations on my entities look like:

@JsonFormat(pattern="yyyy-MM-dd")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalDateTime somedate;

Although that didn't seem to be strictly needed.

Thymeleaf

add dependency:

compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-java8time', version: '3.0.0.RELEASE'

Make sure it matches your Thymeleaf version.

In any HTML in the project your dates fields should now use #temporals instead of #dates. ie:

<td th:text="${#temporals.format(object.somedate, 'yyyy-MM-dd HH:mm')}">12/12/2018</td>

Spring boot

In my Application.java class I added:

@Bean
public Java8TimeDialect java8TimeDialect() {
    return new Java8TimeDialect();
}

The following resources were invaluable:

http://blog.codeleak.pl/2015/11/how-to-java-8-date-time-with-thymeleaf.html#comment-form (Thymeleaf/Springboot)

https://www.thoughts-on-java.org/hibernate-5-date-and-time/ (Hibernate)

Al Grant
  • 2,102
  • 1
  • 26
  • 49
  • For a date-only value without time-of-day, use `LocalDate` rather than `LocalDateTime`. – Basil Bourque Oct 11 '17 at 04:06
  • 1
    While I do not know your broader intentions, let me caution you about using `LocalDateTime` for date-time values. That class purposely lacks any concept of time zone or offset-from-UTC. So that class does *not* represent a moment on the timeline, only a range of potential moments. If you mean to represent a specific moment on the timeline, use the other java.time classes: `Instant`, `OffsetDateTime`, or `ZonedDateTime`. – Basil Bourque Oct 11 '17 at 04:06
  • your answer mentions hibernate but lists jackson dependencies under that. Maybe you're missing something related to hibernate in your answer? – Brian Clozel Oct 11 '17 at 06:50