View Javadoc
1   package pl.matsuo.core.web.mvc;
2   
3   import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
4   import com.fasterxml.jackson.databind.util.ISO8601Utils;
5   
6   import java.text.DateFormat;
7   import java.text.ParseException;
8   import java.text.ParsePosition;
9   import java.text.SimpleDateFormat;
10  import java.util.Date;
11  
12  
13  public class CustomDateFormat extends ISO8601DateFormat {
14    private static final long serialVersionUID = 1L;
15  
16  
17    protected final DateFormat[] formats = new DateFormat[] {
18      new SimpleDateFormat("yy-MM-dd")
19    };
20  
21  
22    @Override
23    public Date parse(String source, ParsePosition pos) {
24      // index must be set to other than 0, I would swear this requirement is not there in
25      // some version of jdk 6.
26      //pos.setIndex(source.length());
27  
28      try {
29        return ISO8601Utils.parse(source, pos);
30      } catch (Exception e) {
31        for (DateFormat dateFormat : formats) {
32          try {
33            return dateFormat.parse(source);
34          } catch (ParseException e1) {
35            // do notin'
36          }
37        }
38  
39        throw new RuntimeException(e);
40      }
41    }
42  }
43