0

I am sending a json request in restful API having a date field (java.util.date in the POJO class), apart from other fields

{
"name" : "abcd";
"enrolldate" : "2016-11-28"
}

I have a validator class as well where i am validating all the input parameters. if validation is true, then only response will be received. Else, need to log error. I need to validate the date field in such a way that it always perform a check that the date is always in the format of yyyy-mm-dd. Can anyone please help.

user2326831
  • 151
  • 2
  • 14
  • 1
    why do you want to do a validation? Given that the method parameter is already java.util.date and the deserializer would take care of converting the input json to the concrete type, it would itself throw an error if the input value (in this case a date) is not right. – Sourabh Nov 27 '16 at 21:31
  • Can you add your POJO class code ? Did you declare it as Date or String ? – Vasu Nov 27 '16 at 21:33
  • if i send the request date in 2016/11/11 format. it says "not a json content". so i need validation. The field i am declaring in pojo is of Date object. – user2326831 Nov 27 '16 at 22:14
  • The `java.util.Date` class is misnamed, representing a date *and* a time-of-day. Your shown data is a date-only value. By the way, the troublesome `Date` and `Calendar` classes are now legacy, supplanted by the java.time classes. Use `LocalDate` for a date-only value. `LocalDate.parse( "2016-11-28" )` – Basil Bourque Nov 27 '16 at 22:44
  • You may use enrolldate as String field in DTO class and then parse it according to your format. – Justinas Jakavonis Nov 28 '16 at 09:00

1 Answers1

0

As mentioned in the comment, given that the pojo has the type for the field as java.util.Date, you should just live that to deserializer to take care of it (whatever it is that you're using). There are multiple valid formats to represent a date and you should probably allow all of them and not restrict to this one particular format. I mean, what's the downside!

Any case, to validate the format and value of the input string date, you could use the java.text.SimpleDateFormat class as follows:

DateFormat formatter1 = new SimpleDateFormat("yyyy-mm-dd");
try {
    Date parsedDate = (Date)formatter1.parse("2016-11-28");
} catch (ParseException e) {
    // invalid format or wrong date 
    e.printStackTrace();
}

exception means that either it's not the same format or an invalid date. Hope that answers your question.

Sourabh
  • 429
  • 1
  • 4
  • 11
  • thanks for the input. one more problem i can see in the server log is that the enrollDate is coming in some random integer value like "14801212000". Can you tell me how to make sure that the date in the logs would also be in the same format as passed in the json request. – user2326831 Nov 29 '16 at 11:57
  • depends on at what stage the log is being written, like is the json being written, or is it after de-serialization. Can you please add the line of code that's writing the log message to understand this better? – Sourabh Nov 29 '16 at 16:43
  • can anyone help for this question : http://stackoverflow.com/questions/41122998/date-is-coming-1-i-e-one-day-less-than-the-actual-in-json?noredirect=1#comment69450012_41122998 – user2326831 Dec 14 '16 at 17:05
  • @Sourabh This solution will be validating date in controller or service layer. Is there any way to add validation in pojo itself – Rishabh Agarwal Oct 04 '21 at 19:04