Spring 3.2 日期时间格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17754849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.2 Date time format
提问by bekur
I am using standalone Spring framework and I want to implement Spring 3.2 @DateTimeFormat(pattern = "dd/mm/yyyy"), but not getting the expected output.
我正在使用独立的 Spring 框架,我想实现 Spring 3.2 @DateTimeFormat(pattern = "dd/mm/yyyy"),但没有得到预期的输出。
My code snippet is:
我的代码片段是:
@DateTimeFormat(pattern = "dd/mm/yyyy")
private Date dob;
public void amehotd(){
Calendar cal;
cal = Calendar.getInstance ();
cal.set (1999, Calendar.AUGUST, 30);
this.dob = cal.getTime();
System.out.println(dob)
}
Gives following result:
给出以下结果:
Mon Aug 30 15:08:14 CDT 1999
but I was expecting output like: 30/08/1999
但我期待输出如下:30/08/1999
I want to implement without joda time library
我想在没有 joda 时间库的情况下实现
回答by NINCOMPOOP
Try changing the format to :
尝试将格式更改为:
@DateTimeFormat(pattern = "dd/MM/yyyy")
MMis for months , mmfor minutes .
MM是几个月,mm几分钟。
Just look at this documentation:
看看这个文档:
The most common ISO DateTime Format yyyy-MM-dd'T'hh:mm:ss.SSSZe.g.
最常见的 ISO 日期时间格式yyyy-MM-dd'T'hh:mm:ss.SSSZeg
回答by amicoderozer
I know it's an old question but I answer because I had the same problem today and I lost 4 hours of work to find the solution. The problem here is spring uses Hymanson to serialize and deserialize JSON. @DateTimeFormatannotation will not do the job, you have to tell Hymanson how to serialize the date. You have two solutions: the first one is the simplier and is to use @JsonFormatannotation in the getter method:
我知道这是一个老问题,但我回答是因为我今天遇到了同样的问题,我浪费了 4 个小时的工作来找到解决方案。这里的问题是 spring 使用 Hymanson 来序列化和反序列化 JSON。@DateTimeFormat注释不会完成这项工作,您必须告诉Hyman逊如何序列化日期。你有两个解决方案:第一个是更简单的,是@JsonFormat在 getter 方法中使用注解:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy")
public Date getDob(){
return dob;
}
The second solution is to create a custom serializer for date fields like this:
第二种解决方案是为这样的日期字段创建自定义序列化程序:
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
and then use the annotation in the get method:
然后在get方法中使用注解:
@JsonSerialize(using=JsonDateSerializer.class)
public Date getDob(){
return dob;
}
this link explains how to do the serializer
这个链接解释了如何做序列化程序
https://dzone.com/articles/how-serialize-javautildate
https://dzone.com/articles/how-serialize-javautildate
I faced another problem, I was importing in my JsonDateSerializer class the classes from org.codehaus.Hymansonpackage, but Spring gived me this error:
我遇到了另一个问题,我在 JsonDateSerializer 类中导入了org.codehaus.Hymanson包中的类,但是 Spring 给了我这个错误:
java.io.FileNotFoundException: class path resource [org/codehaus/Hymanson/map/JsonSerializer.class] cannot be opened because it does not exist
So I changed all the imports to the package
所以我把所有的导入都改成了包
com.fasterxml.Hymanson
and it all works fine. I hope it can help someone.
一切正常。我希望它可以帮助某人。
回答by LaurentG
You are calling System.out.printlndirectly in a method of your class. This can't work that way.
您System.out.println直接在类的方法中调用。这不能那样工作。
You have to call the dobfield from outside of your class, for instance from a JSP page. And this way, the field will be automatically formatted to the given pattern.
您必须dob从类的外部调用该字段,例如从 JSP 页面。这样,该字段将自动格式化为给定的模式。
回答by Ashok Korlakunta
@NumberFormat and @DateTimeFormat annotations work only in Spring MVC environment. You have to use <mvc:annotation-driven/>in your xml mvc configration file.
@NumberFormat 和 @DateTimeFormat 注解仅在 Spring MVC 环境中有效。您必须<mvc:annotation-driven/>在 xml mvc 配置文件中使用。
回答by Jelani
Take a look at your imports in your class. Your date type should be java.util.Date. It might be inheriting from a different class, such as, java.sql.Date.
看看你在课堂上的导入。您的日期类型应该是 java.util.Date。它可能继承自不同的类,例如 java.sql.Date。

