How to bind Java Spring time attribute to Thymeleaf field?

I have a problem binding the Java Time attribute to the Thymeleaf field.

Here is my HTML code

<input th:field="*{startTime}" type="text"/> 
    <script>
$("#startTime").timepicker({
            showSeconds : true,
            showMeridian : false,
            secondStep : 1

        });
    </script>

Here is my model Attribute Code

@NotNull(message = "Start time cannot be empty")
    private Time startTime;

public Time getStartTime() {
        return startTime;
    }

    public void setStartTime(Time startTime) {
        this.startTime = startTime;
    }

When I submit the form, I got an exception

Failed to convert property value of type java.lang.String to required
type java.sql.Time for property startTime; nested exception is
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.sql.Time
for value 14:15:41; nested exception is
org.joda.time.IllegalFieldValueException: Cannot parse "14:15:41":
Value 14 for clockhourOfHalfday must be in the range [1.12]

If I attach 12-hour times, I got this exception

Failed to convert property value of type java.lang.String to required type java.sql.Time for property startTime; nested exception is
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.sql.Time for value 11:15:41; nested exception is
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.joda.time.DateTime to type @javax.validation.constraints.NotNull @org.springframework.format.annotation.DateTimeFormat java.sql.Time

Spring ?

+4
1

String get set

private String startTimeString;

String Time

setStartTime(Time.valueOf(ModelName.getstartTimeString()));
0

Source: https://habr.com/ru/post/1606732/


All Articles