2

I'm trying parse String data to Date.Json get the sending string data for post method.But i have an error.

 private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
private Date date;
@POST
@Produces(MediaType.APPLICATION_JSON+ ";charset=UTF-8")
@Consumes(MediaType.APPLICATION_JSON)
public TravelDTO save(TravelDTO dto) {
    dto.setUser(userService.get(dto.getUser().getUserID()));
     dto.setTravelStart(simpleDateFormat.parse(data));
    return travelService.save(dto);
}

error:

 parse
(java.lang.String)
in DateFormat cannot be applied
to
(java.util.Date)
Thierry
  • 5,270
  • 33
  • 39
real
  • 53
  • 1
  • 2
  • 9
  • Possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Rahul Sharma Apr 27 '17 at 02:33

1 Answers1

1

You are passing wrong type to parse method - it requires String, but data variable has java.util.Date type.

But you are getting confused with SimpleDateFormat class. It doesn't modify Date itself, it's used to parse a String to Date type when used with parse method, or just changes its String representation when used with format method. So, if you need to pass a String to dto.setTravelStart(.) - use simpleDateFormat.format(data). If you need to pass Date instance - pass data object durectly.

Ivan Pronin
  • 1,768
  • 16
  • 14
  • no,i just need incoming string data to database in dd/MM/yyyy format. Because string format is "dd/MM/yyyy" but Date does not accept this – real Apr 26 '17 at 20:31
  • Then my answer should help – Ivan Pronin Apr 26 '17 at 20:33
  • your advice is not worked."Unhandled exception: java.text.ParseException" – real Apr 26 '17 at 20:33
  • This means that `data` variable can't be parsed with your simpleDateFormat. Post its value here – Ivan Pronin Apr 26 '17 at 20:36
  • I don't understand everything look good but i have "parse (java.lang.String) in DateFormat cannot be applied to (com.travelSystem.dto.TravelDTO) " – real Apr 26 '17 at 20:58
  • if i try .format function error = "setTravelStart (java.util.Date) in TravelDTO cannot be applied to (java.lang.String)  " .It's my data = travelService.get(dto.getTravelStart()) and it's Date format. – real Apr 26 '17 at 21:13