格式化日期时间在Thymeleaf“HH:mm”

时间:2017-06-06 11:23:53

标签: spring-mvc thymeleaf

我需要使用格式掩码,但我只能使用DATE类型'dd-MMM-yyyy',但我需要时间格式'HH:mm'。

//ENTITY DateTimeFormat annotation on the method that's calling the DB to get date-time.
@Entity
@Table(name="horario")
public class Horario implements Serializable {} ....

@NotNull
@Column(name = "duracao")
@Temporal(TemporalType.TIME)
@DateTimeFormat(pattern ="HH:mm")
private Date duracao;


@NotNull
@Column(name ="hora_inicial")
@Temporal(TemporalType.TIME)
@DateTimeFormat(pattern = "HH:mm")
private Date horaInicial;

@NotNull
@Column(name ="hora_final")
@Temporal(value = TemporalType.TIME )
@DateTimeFormat(pattern = "HH:mm")
private Date horaFinal;

并且用百里香活力充满活力

我认为百里香与javascript等角度无法很好地互动

//HTML 
 <div class="form-group" th:each="horarios, rowStat : *{horarios}" th:if="${rowStat.index} &lt; 5">
                                <div class="col-sm-4 col-md-4 col-4">
                                    <div class="form-group"
                                         th:classappend="${#fields.hasErrors('horarios[__${rowStat.index}__].duracao')} ? has-error">
                                        <label class="control-label required" th:text="#{mensagem.duracao}">Duração</label>
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="fa fa-clock-o"></i>
                                            </span>
                                            <input type="text" class="form-control" th:id="duracao+${rowStat.index}" th:field="*{{horarios[__${rowStat.index}__].duracao}}"
   I'm starting with this div, 
once it's resolved, I've moved on to the others-------------> th:value="${#dates.format(horarios.duracao, 'HH:mm')}" />
                                        </div>
                                        <label class="error" th:if="${#fields.hasErrors('horarios[__${rowStat.index}__].duracao')}"
                                               th:errors="*{horarios[__${rowStat.index}__].duracao}"></label>
                                    </div>
                                </div>

                                <div class="col-sm-4 col-md-4 col-4">
                                    <div class="form-group"
                                         th:classappend="${#fields.hasErrors('horarios[__${rowStat.index}__].horaInicial')} ? has-error">
                                        <label class="control-label required"
                                               th:text="#{mensagem.horaInicial}">horaInicial</label>
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                    <i class="fa fa-clock-o"></i>
                                                </span>
                                            <input type="text" class="form-control" th:id="horaInicial+${rowStat.index}"
                                                   th:field="*{horarios[__${rowStat.index}__].horaInicial}"
                                                  />
                                        </div>
                                        <label class="error" th:if="${#fields.hasErrors('horarios[__${rowStat.index}__].horaInicial')}"
                                               th:errors="*{horarios[__${rowStat.index}__].horaInicial}"></label>
                                    </div>
                                </div>

                                <div class="col-sm-3 col-md-3 col-3">
                                    <div class="form-group"
                                         th:classappend="${#fields.hasErrors('horarios[__${rowStat.index}__].horaFinal')} ? has-error">
                                        <label class="control-label required"
                                               th:text="#{mensagem.horaFinal}">horaFinal</label>
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="fa fa-clock-o"></i>
                                            </span>
                                                <input type="text" class="form-control" th:id="horaFinal"
                                                       th:field="*{horarios[__${rowStat.index}__].horaFinal}" readonly="readonly"/>
                                        </div>
                                        <label class="error" th:if="${#fields.hasErrors('horarios[__${rowStat.index}__].horaFinal')}"
                                               th:errors="*{horarios[__${rowStat.index}__].horaFinal}"></label>
                                    </div>
                                </div>
                                <div clas="col-sm-1">
                                    <button class="btn btn-danger padding-buttons" type="submit" name="removeRow" th:value="${rowStat.index}" th:if="${rowStat.index} ge 0 and ${#lists.size(horarioAula.horarios) ne 1}">
                                        <i class="fa fa-minus" style="color:white;"></i>
                                    </button>

                                </div>
                        </div>

我的回报是这样的 enter image description here

1 个答案:

答案 0 :(得分:0)

要使用@DateTimeFormat注释,请尝试使用双括号语法:

th:value="${{horarios.duracao}}"

或者,您可以使用th:field属性,此转换将自动进行。

相关文档:http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#double-brace-syntax