Spring 3.2 Date Format

I am using the standalone Spring framework and I want to implement Spring 3.2 @DateTimeFormat (pattern = "dd / mm / yyyy") but not get the expected result.

My code snippet:

@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) } 

It gives the following result:

 Mon Aug 30 15:08:14 CDT 1999 

but I expected an exit like: 08/30/1999

I want to implement without joda time library

+8
spring
source share
5 answers

Try changing the format:

 @DateTimeFormat(pattern = "dd/MM/yyyy") 

MM is in a few months, MM in a few minutes.

Take a look at this documentation :

The most common ISO format is DateTime yyyy-MM-dd'T'hh: mm: ss.SSSZ , for example.

+15
source share

I know this is an old question, but I answer, because today I have the same problem and I lost 4 hours of work to find a solution. The problem here is spring uses jackson to serialize and deserialize JSON. @DateTimeFormat annotation will not do the job, you have to tell Jackson how to serialize the date. You have two solutions: the first one is simpler and should use the @JsonFormat annotation in the getter method:

 @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/MM/yyyy") public Date getDob(){ return dob; } 

The second solution is to create your own serializer for the date fields, for example:

 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:

 @JsonSerialize(using=JsonDateSerializer.class) public Date getDob(){ return dob; } 

this link explains how to make a serializer

https://dzone.com/articles/how-serialize-javautildate

I had another problem: I imported classes from the org.codehaus.jackson package into my JsonDateSerializer class, but spring gave me this error:

 java.io.FileNotFoundException: class path resource [org/codehaus/jackson/map/JsonSerializer.class] cannot be opened because it does not exist 

So, I changed all the import data in the package

 com.fasterxml.jackson 

and everything works fine. Hope this can help someone.

+5
source share

You call System.out.println directly in the method of your class. It may not work like that.

You must call the dob field outside of your class, for example, from a JSP page. And thus, the field will be automatically formatted for this template.

+1
source share
Annotations

@NumberFormat and @DateTimeFormat only work in Spring MVC environment. You should use <mvc:annotation-driven/> in the xml mvc configuration file.

0
source share

Take a look at your imports in your class. Your date type should be java.util.Date. It can be inheritance from another class, for example, java.sql.Date.

0
source share

All Articles