以百里香形式查看日历对象作为日期类型 [英] view Calendar Objects in thymeleaf forms as date types

查看:143
本文介绍了以百里香形式查看日历对象作为日期类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户对象,并且用户有一个字段DOB(出生日期),我已经将该字段作为日历存储在用户BO内.

I have a user object, and the User is having a field DOB (Date of Birth) I have stored that field as a calendar inside the User BO.

类似这样的东西:

公共类UserBO {

public class UserBO {

private Calendar dateOfBirth;

public Calendar getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(Calendar dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

}

现在,我需要将此字段显示为百里香中的日期字段,而不是文本字段.我需要一个日期字段,因为我喜欢日期选择器:)

Now I need to display this field as a Date field in thymeleaf and not a text field. I need a date field since I like the date picker :)

这是我到目前为止所拥有的

This is what I have so far

<label class="col-xs-2">Date of Birth</label>
    <div class="col-xs-2">
    <input type="date" class="form-control" th:field="*{dateOfBirth}" placeholder="Date of Birth" />
    </div>  

但这给我的输出为

这不是我所期望的.我期望从服务中填充一个实际日期,但是如上所述.

this is not what I am expecting. I am expecting an actual date to be populated from the service but its showing me this as above.

我已经阅读了有关Seralization&对Calendar对象进行反序列化并编写某种转换器,但是我没有得到完整的上下文说明为什么需要这样做.另外,当我看过

I have read about Seralization & deserialization of Calendar objects and writing some sort of converters but I did not get full context why is this required. plus when I have seen examples with

输入type ="text"和日期已正确填充.所以有人可以指导我进行这种转换的基础是什么,如何执行此转换的示例将是很好的.

input type="text" and the dates are populated correctly. SO can some one please guide me what is the fundamentals for this conversion and and example of how this should be done would be nice.

谢谢

推荐答案

在使用类型为date的输入元素时,日期值应始终设置为yyyy-mm-dd.将根据用户浏览器的设置区域选择显示的日期格式.

When you are using input elements of type date, the date value should be always formatted as yyyy-mm-dd. The displayed date format will be chosen based on the set locale of the user's browser.

尝试以下代码:

  <input type="date" value="10/14/2016" class="form-control" id="dateOfBirth"
              th:value="${#calendars.format(dateOfBirth,'yyyy-MM-dd')}" th:name="dateOfBirth" />

这篇关于以百里香形式查看日历对象作为日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆