字符串解析日期

时间:2020-07-16 13:33:24

标签: java

我需要调用和api,它们需要采用以下格式的日期

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

我正在尝试以下代码

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

但是我遇到了以下异常

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]]

0 个答案:

没有答案
相关问题