-1

I am trying to convert a JSON present inside a file into a Java Object using ObjectMapper.

JSON file: sample-date-str.json

{
    "eventDate"="2017-06-27 10:04:26.573503+05:30"
}

Java POJO: SampleDatePOJO

class SampleDatePOJO{

    private Date eventDate;

    getter and setter....

    toString...
}

Java code for converting JSON to Java object:

ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSX")); //this will convert Date to IST format which is okay.
SampleDatePOJO sampleDatePOJO = mapper.readValue(new File("sample-date-str.json"), SampleDatePOJO.class);
System.out.println(sampleDatePOJO.getEventDate()); //this gives output "Tue 
Jun 27 10:43:59 IST 2017"

Note: If I remove mapper.setDateFormat(..) and use @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSSX", timezone="IST") on 'eventDate' field it gives the same output.

Java Code for custom Deserilaizer

public class CustomDateDeserializer extends JsonDeserializer<Date>{

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSXXX");

        Date dateTmp = null;
        try {
            dateTmp = sdf.parse(p.getValueAsString());
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return dateTmp;
    }
}

If I use @JsonDeserialize annotation annotation above 'eventDate' field as:

@JsonDeserialize(using=CustomDateDeserializer.class)

The output received is:

System.out.println(sampleDatePOJO.getEventDate()); //"Tue Jun 27 10:13:59 IST 2017"

which is correct as the millisecond "573503" part is added to the Date.

I need to understand why my ObjectMapper setDateFormat and @JsonFormat is not working. Or if they are working fine why there is a difference in the output with same SimpeDateFormat format. Any help is greatly appreciated.

SachinSarawgi
  • 2,632
  • 20
  • 28
  • 1
    But why do you want to perceive the 573503 as milliseconds? Weren’t they intended as microseconds (millionths of seconds) from the other end? Related: [java.text.ParseException: Unparseable date: yyyy-MM-dd HH:mm:ss.SSSSSS](https://stackoverflow.com/questions/8607809/java-text-parseexception-unparseable-date-yyyy-mm-dd-hhmmss-ssssss). – Ole V.V. Aug 28 '17 at 09:53
  • 1
    Take a look at [`SimpleDateFormat` javadoc](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#iso8601timezone): **if the pattern is "X" and the time zone is "GMT+05:30", "+05" is produced**. With only one `X`, the 30 minutes in the offset are ignored and `SimpleDateFormat` messes everything up. Also, as @OleV.V. said in the above comment, are you sure you really want to consider 573503 as milliseconds and add it to the date? This doesn't sound the more correct approach, because everything after the decimal point should be considered the fractions of the second. –  Aug 28 '17 at 12:14

1 Answers1

2

Have you noticed, In first two conversions you are using single X (yyyy-MM-dd HH:mm:ss.SSSSSSX) in date pattern. But in custom de-serailizer you are using proper XXX (yyyy-MM-dd HH:mm:ss.SSSSSSXXX).

This pattern difference is actually causing all the difference (the difference of 30 minutes). if you use same pattern across different mechanism then output would also remain same.

Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45