0

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?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Estudeiro
  • 383
  • 2
  • 4
  • 18
  • 1
    Why do you use `Date.toString()` in first place? why to do not use standard format for communications - ISO-8601? – Vadim Oct 15 '18 at 18:52
  • Because the map obligates me. I have to make Date.toString() to be able to put the date inside the map – Estudeiro Oct 15 '18 at 18:57
  • Not a correct answer: ISO-8601 is a String format for Date/Time which is more suitable to pass Date/Time as String – Vadim Oct 15 '18 at 19:00
  • Maybe you can make the second type parameter of the hashmap a LocalDateTime type instead of a String type? – wenzi Oct 15 '18 at 20:13

1 Answers1

0

To fix this Issue you have a couple of options:

1 Use Locale or a DateTime-Formatter

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

2 Send another data-Type

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.

duffy356
  • 3,678
  • 3
  • 32
  • 47
  • Thx duffy, for your answer. But i've tried to map the date like you said, and didn't work, i've kept receiving bad request error. – Estudeiro Oct 17 '18 at 13:55
  • does it work, if you remove the second parameter? There might be another error in your endpoint, so you could remove the date-parameter and try again - if the error is still there, the date-parameter is not the problem – duffy356 Oct 18 '18 at 12:33