0

I need to invoke and api, which needs date in the below format

 "createdOn": "2020-07-16T13:30:29.470Z",

I am trying the below code

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String createdDate = simpleDateFormat.format(Calendar.getInstance().getTime());
System.out.println("createdDate :::::" + createdDate);

but I am getting the below exception

2020-07-16 18:58:10.034  WARN 15244 --- [nio-9085-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2020-07-16 18:58:10": not a valid representation (error: Failed to parse Date value '2020-07-16 18:58:10': Cannot parse date "2020-07-16 18:58:10": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-07-16 18:58:10": not a valid representation (error: Failed to parse Date value '2020-07-16 18:58:10': Cannot parse date "2020-07-16 18:58:10": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
 at [Source: (PushbackInputStream); line: 1, column: 74]]
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • 1
    **(1)** You are using an outdated and troublesome API for date and time operations, **(2)** your pattern doesn't match the formatting of the `String` value you are trying to parse (hint: missing `T` between date and time along with missing parsing option for milliseconds and the offset or time zone isn't considered) – deHaar Jul 16 '20 at 13:35
  • 2
    Please stop using `SimpleDateFormat` and `Calendar`. Use the newer Java Date and Time API available in the `java.time` package. – MC Emperor Jul 16 '20 at 13:35
  • 2
    Your format is missing millis and time zone – Stewart Jul 16 '20 at 13:36
  • the exact matching pattern is written right in the exception: `while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ'` – jhamon Jul 16 '20 at 13:43
  • Try: `System.out.println(Instant.now());` using Java 8 or later. – Andreas Jul 16 '20 at 14:05

0 Answers0