Spring boot 如何使用不同的日期/时间格式在thymeleaf中输入和人类可读文本输出

Spring boot 如何使用不同的日期/时间格式在thymeleaf中输入和人类可读文本输出,spring-boot,internationalization,thymeleaf,Spring Boot,Internationalization,Thymeleaf,我用的是弹簧靴和百里香 对于html表单日期输入字段,我必须使用2019-10-27日期格式。对于人类可读的文本,我更喜欢使用适合本地环境的文本,例如2019年10月27日或2019年10月27日 Spring Boot和Thymeleaf使指定@DateTimeFormat和自动在th:field等字段中拾取它变得很容易。在这里,我指定了技术格式,否则我的表单既不能正确呈现也不能正确提交 有没有办法让Thymeleaf或Spring在其他场合使用不同的格式?我知道我可以做${{#tempora

我用的是弹簧靴和百里香

对于html表单日期输入字段,我必须使用2019-10-27日期格式。对于人类可读的文本,我更喜欢使用适合本地环境的文本,例如2019年10月27日或2019年10月27日

Spring Boot和Thymeleaf使指定
@DateTimeFormat
和自动在
th:field
等字段中拾取它变得很容易。在这里,我指定了技术格式,否则我的表单既不能正确呈现也不能正确提交


有没有办法让Thymeleaf或Spring在其他场合使用不同的格式?我知道我可以做
${{#temporals.format(period.end,#messages.msg('formats.readable.date.month'))}
,但是有没有办法注册这个东西并使其可用于自动转换?

你也可以使用
WebDataBinder

@InitBinder
public void initBinder(WebDataBinder binder, final Locale locale) {
    String dateFormat = "MM/dd/yyyy";
    if(locale.getLanguage.equals("ar")){
        dateFormat = "dd-MM-yyyy";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    sdf.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}