1

I have a problem, I want to parse to different type of date string to timestamp.

I used

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern ="dd-MMM-yyyy HH:mm:ss.SSS       z",timezone="IST")
private Timestamp validUptoDate;

and parsing is Ok when milisecond value is given after second, But if I provide "16-DEC-2014 15:20:30 IST" this type of date, getting below exception ::

Failed to parse Date value '15-AUG-2019 19:30:00 IST' (format: "dd-MMM-yyyy HH:mm:ss.SSS z"): Unparseable date: "15-AUG-2019 19:30:00 IST"

But If I provide "16-DEC-2014 15:20:30.000 IST" , then no issues. But I dont want to mention milisecond value if not present.

I tried to provide two pattern value llike

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern ={"dd-MMM-yyyy HH:mm:ss z","dd-MMM-yyyy HH:mm:ss.SSS z"} ,timezone="IST")

but compilation error happens. I tried using different combination, but no hope. Please help. Thanks a lot.

Ghokun
  • 3,345
  • 3
  • 26
  • 30
sayan
  • 501
  • 2
  • 7
  • 21
  • have you tried removing .SSS part from HH:mm:ss.SSS ? @JsonFormat(shape=JsonFormat.Shape.STRING, pattern ="dd-MMM-yyyy HH:mm:ss z",timezone="IST") – Ghokun Dec 16 '14 at 13:40
  • yes, it worked when I removed .SSS part. But I want to parse two type of pattern simultaneously, is there a way to do that ? – sayan Dec 17 '14 at 05:52
  • By looking at the javadoc pattern only accepts "string" values not array like you provided. But it does not say anything about regular expressions. Try something like pattern="pattern1|pattern2" and we will know if it accepts regex values. – Ghokun Dec 17 '14 at 06:58
  • i tried using pattern="pattern1|pattern2", but no improve ! :( – sayan Dec 17 '14 at 10:52
  • Got 1 useful link : http://stackoverflow.com/questions/5591967/jackson-date-deserialization – sayan Dec 17 '14 at 11:30
  • By reading that link I guess you can solve it by creating a custom object mapper and setting default date format to ISO8601DateFormat(). If you don't know how to do that I can provide more details as answer. – Ghokun Dec 17 '14 at 12:01
  • also got another link for customised serialization :: http://stackoverflow.com/questions/7161638/how-do-i-use-a-custom-serializer-with-jackson – sayan Dec 17 '14 at 12:43

0 Answers0