JPA support for Java 8 new date and time API

For Hibernate 5.X just add <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-java8</artifactId> <version>${hibernate.version}</version> </dependency> and @NotNull @Column(name = “date_time”, nullable = false) protected LocalDateTime dateTime; will work without any additional effort. See https://hibernate.atlassian.net/browse/HHH-8844 UPDATE: Please have a look at Jeff Morin comment: since Hibernate 5.2.x it is enough <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.1.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-…</artifactId> <version>4.3.1.RELEASE</version> </dependency> See …

Read more

Java 8 – DateTimeFormatter and ISO_INSTANT issues with ZonedDateTime

The ISO_INSTANT formatter is documented here – “This is a special case formatter intended to allow a human readable form of an Instant”. As such, this formatter is intended for use with an Instant not a ZonedDateTime. Formatting When formatting, ISO_INSTANT can format any temporal object that can provide ChronoField.INSTANT_SECONDS and ChronoField.NANO_OF_SECOND. Both Instant and …

Read more

How to create Java time instant from pattern?

The string “9999-12-31” only contains information about a date. It does not contain any information about the time-of-day or offset. As such, there is insufficient information to create an Instant. (Other date and time libraries are more lenient, but java.time avoids defaulting these values) Your first choice is to use a LocalDate instead of an …

Read more

Java 8 LocalDate – How do I get all dates between two dates?

Assuming you mainly want to iterate over the date range, it would make sense to create a DateRange class that is iterable. That would allow you to write: for (LocalDate d : DateRange.between(startDate, endDate)) … Something like: public class DateRange implements Iterable<LocalDate> { private final LocalDate startDate; private final LocalDate endDate; public DateRange(LocalDate startDate, LocalDate …

Read more

`uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?

Within the scope of java.time-package, we can say: It is safer to use “u” instead of “y” because DateTimeFormatter will otherwise insist on having an era in combination with “y” (= year-of-era). So using “u” would avoid some possible unexpected exceptions in strict formatting/parsing. See also this SO-post. Another minor thing which is improved by …

Read more