About Rafal Borowiec

Rafal is an IT specialist with about 8 years of commercial experience, specializing in software testing and quality assurance, software development, project management and team leadership.

Spring 4: @DateTimeFormat with Java 8 Date-Time API

logo-spring-io

@DateTimeFormat annotation that was introduced in Spring 3.0 as a part of Formatter SPI can be used to to parse and print localized field values in web applications. In Spring 4.0, @DateTimeFormat annotation can be used with Java 8 Date-Time API (java.time) out-of-the-box, without extra effort.

In Spring, field formatting can be configured by field type or annotation. To bind an annotation to a formatter AnnotationFormatterFactory must be implemented. Spring 4.0 brings Jsr310DateTimeFormatAnnotationFormatterFactory that formats Java 8 Date-Time fields annotated with the @DateTimeFormat. Supported field types are as follows:

  • java.util.LocalDate
  • java.util.LocalTime
  • java.util.LocalDateTime
  • java.util.ZonedDateTime
  • java.util.OffsetDateTime
  • java.util.OffsetTime

One can utilize all mentioned types in a form like below:

public class DatesForm {

    @DateTimeFormat(iso = ISO.DATE)
    private LocalDate localDate;

    @DateTimeFormat(iso = ISO.TIME)
    private LocalTime localTime;

    @DateTimeFormat(iso = ISO.TIME)
    private OffsetTime offsetTime;

    @DateTimeFormat(iso = ISO.DATE_TIME)
    private LocalDateTime localDateTime;

    @DateTimeFormat(iso = ISO.DATE_TIME)
    private ZonedDateTime zonedDateTime;

    @DateTimeFormat(iso = ISO.DATE_TIME)
    private OffsetDateTime offsetDateTime;
    
}

The form can be passed to the view and Spring will take care of proper formatting of the fields.

datetimeformat-spring-4-java-8-1

While specifying the formatting on the fields of types: java.util.LocalDate, java.util.LocalTime, java.util.OffsetTime you need to remember to properly configure @DateTimeFormat.

@DateTimeFormat declares that a field should be formatted as a date time and since java.util.LocalDate represents a date, and the other two represent time – you will get java.time.temporal.UnsupportedTemporalTypeException (e.g.: Unsupported field: ClockHourOfAmPm, Unsupported field: MonthOfYear) thrown by java.time.format.DateTimeFormatter.

Reference: Spring 4: @DateTimeFormat with Java 8 Date-Time API from our JCG partner Rafal Borowiec at the Codeleak.pl blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


three − 1 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close