0

I have scenario where i need to store the dynamic attributes into the database in the form of HashMap given by the user using a rest service. This dynamic attributes is defined as

HashMap<String, Object> attr = new HashMap<>();

Now, I have to convert the object into their respective run time data types according to the business logic. So far, I am able to succeed in converting strings, numbers but Date is causing trouble. This is my conversion logic.

input = attr.get(label);
if (input instanceof String) {
      // logic 1
 } 
else if (input instanceof Number) {
      // logic 2
 }
else if (input instanceof Date)  {
      // logic 3
 } 

The problem is when date is given in the any date format (in my case zulu format) the jacksonjson converter is unable to parse it and throws error stating cannot parse the input string. I understand that it had been a Date object. I could have written a JsonDeserializer and annotate the Date object with it, but since it is a Object within a HashMap how should i achieve this.

I have also tried catching that date string and parsing it using a SimpleDateFormat but failed since the exception occurs way before this logic.

4
  • If you don't know some field is a date there is not much you can do. How would you distinguish a date value from a string value that happens to look like a date? Commented Feb 14, 2019 at 5:03
  • I agree. For the same reason, I have written the logic so as to catch it as string and parse it using SimpleDateFormat. But jackson cannot convert it into string also, that is the problem. Commented Feb 14, 2019 at 5:05
  • What are the possible date formats you can get in this object value? Commented Feb 14, 2019 at 5:11
  • Could you provide small example which recreates this bug with small JSON payload and how to you deserialise it? If you use Map<String, Object> as result from deserialisation process there is no possibility that Jackson will find out that this is a date. It should be a String. Commented Feb 14, 2019 at 8:28

2 Answers 2

1

if you are parsing date from json, you probably should do this. In JSON, your date is string. you have to convert your date which is string to actual date format using SimpleDateFormat. It will not find, input instanceof Date, and you can't parse your json to actual date. For example, if your json look like this,

{'name':'firstname','date':'14/02/2019'}

You can now convert date to actual date like this

SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");


Date date1=formatter1.parse(input); //here input is taken from your case which represent date  
Sign up to request clarification or add additional context in comments.

Comments

0

If you're JDK is modern -- read 8+, you can use java.time.DateFormat as follows:

java.time.format.LocalDate date = java.time.format.LocalDate.parse(ourDateAsString, java.time.format.DateTimeFormat.BASIC_ISO_DATE);
java.util.Date dateObject = java.util.Date.from(date.atStartOfDay()
      .atZone(java.time.ZoneId.systemDefault())
      .toInstant());

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.