0

In my Spring Boot application, I need to handle a form with date time field and convert it to LocalDateTime in Java.

I specified pattern "YYYY-MM-dd HH:mm" and it fails to convert when I submit the form with input value 1990-01-01 10:10.

Here is the form object:

public class UserForm {
    @NotNull
    @DateTimeFormat(pattern = "YYYY-MM-dd HH:mm")
    private LocalDateTime dateTime;
    // getters, setters
}

Controller:

@RequestMapping("/users")
@Controller
public class UserController {
    @GetMapping("")
    public String userForm(UserForm userForm) {
        return "/users/form";
    }

    @PostMapping("")
    public String postForm(@Valid UserForm userForm, BindingResult bindingResult) {
        System.out.println(userForm + " " + bindingResult);
        return "/users/form";
    }
}

And Thymeleaf form:

<form th:object="${userForm}" th:action="@{/users}" method="post">
    <span th:each="err: ${#fields.errors('dateTime')}" th:text="${err}" style="background-color:red;color:white;"/>
    <input type="text" th:field="*{dateTime}"/>
    <input type="submit">
</form>

What is wrong with this example ? How should I fix it to make it properly parse String to LocalDateTime ?

I also submitted example application here.

Update:

  • Under "fails to convert" I mean I'm getting exception:

    org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value 1990-01-01 10:10

  • Using lowercase yyyy fixed the problem. Thanks.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Oleksandr.Bezhan
  • 2,028
  • 3
  • 22
  • 31
  • 1
    Define "fails to convert". And use the correct pattern. YYYY is the week year. You want the year. yyyy. – JB Nizet Jul 15 '18 at 14:00

1 Answers1

5

Calendar year, not week-based year

Formatting pattern should be uuuu-MM-dd HH:mm.

LocalDateTime.parse( 
    "1990-01-01 10:10" , 
    DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm" )
)

The uppercase YYYY means the year of a week-based year rather than a calendar-year.

Study the class documentation more carefully for formatting pattern codes. Notice how they are case-sensitive.

ISO 8601

Tip: Instead of using a custom format, stick with using standard ISO 8601 formats.

The java.time classes use the standard formats by default. So no need to bother with specifying a formatting pattern.

LocalDateTime.parse( 
    "1990-01-01T10:10"
)

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154