11

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.

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

I want to implement without joda time library

Chris McFarland
  • 6,059
  • 5
  • 43
  • 63
AKB
  • 5,918
  • 10
  • 53
  • 90
  • How do you "print" that output? I ask because it looks like the annotation is not taken in account at all. – Ralph Jul 19 '13 at 21:57
  • I would use the [Joda-time](http://joda-time.sourceforge.net/) API (as it is always my preference) and certainly not the Java SE default date time APIs. If you're using Spring 3.2, then you can register a custom property editor directly with [`@ControllerAdvice`](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html) to meet whatever you need. – Lion Jul 20 '13 at 14:31

5 Answers5

22

Try changing the format to :

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

MM is for months , mm for minutes .

Just look at this documentation:

The most common ISO DateTime Format yyyy-MM-dd'T'hh:mm:ss.SSSZ e.g.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 3
    @DateTimeFormat(pattern = "dd/MM/yyyy") private Date dob; but still getting: Mon Aug 30 15:28:22 CDT 1999 – AKB Jul 19 '13 at 20:29
14

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 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 the simplier and is to use @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 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:

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

this link explains how to do the serializer

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

I faced another problem, I was importing in my JsonDateSerializer class the classes from org.codehaus.jackson package, but Spring gived 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 imports to the package

com.fasterxml.jackson

and it all works fine. I hope it can help someone.

amicoderozer
  • 2,046
  • 6
  • 28
  • 44
1

You are calling System.out.println directly in a method of your class. This can't work that way.

You have to call the dob field from outside of your class, for instance from a JSP page. And this way, the field will be automatically formatted to the given pattern.

LaurentG
  • 11,128
  • 9
  • 51
  • 66
1

@NumberFormat and @DateTimeFormat annotations work only in Spring MVC environment. You have to use <mvc:annotation-driven/> in your xml mvc configration file.

0

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.

Jelani
  • 705
  • 6
  • 25