在thymeleaf的本地时间有什么问题

evrscar2  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(375)

spring+thymeleaf项目的本质描述如下:

@Entity
public class CarEntity {
...
    @Column(name = "time_oot", nullable = false)
    //@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")
    //@DateTimeFormat(pattern = "hh:mm:ss")

    private LocalTime timeOut;
}

我们从html表单中读取localtime格式的时间。我的输出没有秒,一切正常。ie表单输入的类型如下:

<label for="inputime">Время</label>
<input type="time" class="form-control" id="inputime" placeholder="Задайте время" max="0:50:00" min="0:00:01" value="00:00" th:field="*{timeOut}" required>

但我不需要在表格上写上几个小时,只需要几分钟和几秒钟,我在表格上加了几秒钟:

<label for="inputime">Время</label>
<input type="time" class="form-control" id="inputime" placeholder="Задайте время" max="0:50:00" min="0:00:01" value="00:00:00" step="1" th:field="*{timeOut}" required>

现在错误开始蔓延开来:

Field error in object 'CarEntity' on field 'timeOut': rejected value [00:12:25]; 
codes 
[typeMismatch.CarEntity.timeOut,typeMismatch.timeOut,typeMismatch.java.time.LocalTime,typeMismatch]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [CarEntity.timeOut,timeOut]; 
arguments []; default message [timeOut]]; 
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalTime' for property 'timeOut'; 
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] 
to type [@javax.persistence.Column java.time.LocalTime] for value '00:12:25'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [00:12:25]]]

有什么问题?
我试着加上:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "hh:mm:ss")

@DateTimeFormat(pattern = "hh:mm:ss")

错误仍然崩溃:

"Parse attempt failed for value [01:11:23]"
tjjdgumg

tjjdgumg1#

使用 @DateTimeFormat(pattern = "HH:mm:ss") 您可以通过以下方式进行测试:

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        LocalTime localTime = LocalTime.parse("00:12:25", DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println("localTime = " + localTime);
    }
}

使用时 hh:mm:ss ,此代码在使用 HH:mm:ss 它起作用了。 HH 是你需要用的24小时记数法。

相关问题