I'm currently with this problem. I have a Date.toString()
. On one side, I want to send this to some other side as a Date
. How to do this?
Here is my code:
public void createEmployee(Employee emp) throws HelicarrierException {
log.debug(LOG_PREFIX + "[SERVICE]-[CREATE-LOG-GOAL] - [GOAL] - [{}]", emp);
HashMap<String, String> map = new HashMap<>();
map.put("nameEmployee", emp.getName());
map.put("admissionalDate", emp.getAdmissionDate().toString());
RestTemplate rt = new RestTemplate();
String url = "http://" + properties.getSktServerEndpoint()
+ ":" + properties.getSktServerPort()
+ properties.getModelEmployee()
+ properties.getCreateEmployee();
log.debug(LOG_PREFIX + "-[URL]-[{}]", url);
log.debug(LOG_PREFIX + "-[PARAMETERS]-[{}]", map);
try {
rt.postForObject(url, emp, Employee.class, map);
}
Note that emp.getAdmissionDate().toString() = Mon Oct 15 00:00:00 BRT 2018
And I'm trying to map this parameter on the other side like this:
@RequestMapping(method = RequestMethod.POST, path = "/create", produces = "application/json")
public ResponseEntity<GenericResponse> create(@RequestParam(value = "nameEmployee") String nameEmployee, @RequestParam(name = "admissionalDate") @DateTimeFormat(pattern="dow mon dd hh:mm:ss zzz yyyy)) Date admissionalDate)
But I always get, Bad Request error. I think the Date
mapping is wrong, but I don't know how to do it right. Anyone know, how to map properly, this string to date?
To fix this Issue you have a couple of options:
Use a DateTime-Formatter to send the String-Representation of the date and parse it APPROPRIATE on the other side to a date.
A very common Format for Timestamps is as already mentioned ISO-8601
you could send directly Date(s) or DateTimes
In your case it seems like the @DateTimeFormat(pattern="dow mon dd hh:mm:ss zzz yyyy)
is incorrect.
Your output string is formatted as Mon Oct 15 00:00:00 BRT 2018
. The appropriate format for this String on the other side would be EEE MMM dd hh:mm:ss zzz yyyy
.
For more information take a look inside the DateTimeFormatter-Docs .
Or take a look at another posts .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.