English 中文(简体)
使用 Spring MVC 设定输入文字格式的日期
原标题:Set date format for an input text using Spring MVC

如何在 Spring MVC 的文本字段中设置 < a href=" < a href=" https://docs.oracle.com/javase/8/docs/api/java/ util/ Date.html" rel=" nofollow noreferr"\\code > Date 的格式?

我使用 Spring Form 标记库和 input 标记库。

我现在得到的就是这个 Mon 5月28日 11:09:28 CEST 2012

我想用 dd/MM/yyyyy 格式显示日期。

最佳回答

在 yr 控制器中注册日期编辑器 :

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(LocalDate.class, new LocalDateEditor());
}

然后数据编辑器本身可以看起来像这样 :

public class LocalDateEditor extends PropertyEditorSupport{

 @Override
 public void setAsText(String text) throws IllegalArgumentException{
   setValue(Joda.getLocalDateFromddMMMyyyy(text));
 }

 @Override
 public String getAsText() throws IllegalArgumentException {
   return Joda.getStringFromLocalDate((LocalDate) getValue());
 }
}

我用我自己的抽象工具类(Joda)来解析日期, 事实上本地数据来自 Joda Datetime 图书馆 - 推荐, 因为标准 java 日期/ calendar 是令人憎恶的, imho。 但是您应该明白这一点。 您也可以注册一个全球编辑, 这样您就不必做每个控制器( 我记不清如何操作 ) 。

问题回答

完成了! 我刚把这个方法添加到我的控制器类中 :

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签