I'm using Spring Framework for my services API and org.joda.time.DateTime
for datetime parsing. Specifically, I'm using the ISOFormatter.dateOptionalTimeParser()
, which allows users the flexibility to use just the date, or both date and time, which is a requirement.
Believe me, I've seen all these related questions that I can already tell people are going to point me towards, e.g. this and this, etc.
Previously, I was taking the date as String
and then processing it using the joda formatter mentioned above in the service layer, but now I want to add request validation in the controller, which means that if the request is syntactically incorrect, the request shouldn't even go to the service layer.
I've tried using multiple variations of @DateTimeFormat(iso = ISO.DATE_TIME)
, as well as specifying the pattern
String in format thing with no luck, whatsoever.
@RequestMapping(value = URIConstants.TEST_URL, method = RequestMethod.GET)
public @ResponseBody String getData(@RequestParam(required = false) DateTime from,
@RequestParam(required = false) DateTime to) {
return dataService.fetchDataFromDB(from, to);
}
What should I do to ensure that the date I get from user complies with the ISO 8601 dateOptionalTime
format? Can I maybe apply multiple patterns to implement this?