开发者

Error for formatting Cannot format given Object as a Date [duplicate]

This question already has answers here: Java : Cannot format given Object as a Date (7 answers) How to sanity check a date in Java (24 answers) Illegal pattern character 'T' when parsing a date string to java.util.Date (4 answers) java.text.ParseExcep开发者_开发技巧tion: Unparseable date "yyyy-MM-dd'T'HH:mm:ss.SSSZ" - SimpleDateFormat (3 answers) Closed 5 hours ago.

I have this code:

private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

Now, I try to format check some String data,

final String format = SIMPLE_DATE_FORMAT.format("2022-12-07T10:57:23.970Z");

I get the error that Cannot format the given Object as a Date. Whats the issue here and how do I resolve it?


There are two issues with your code snippet:

  1. The format method formats a Date object to String. Perhaps, you want to use the parse method which will convert a String to a Date object.
  2. The String you passed is not compliant with the pattern you passed. The Z in the pattern mandates that a timezone should be passed. A valid example would be -0700. An example of this is shown in the javadoc for SimpleDateFormat; just search with yyyy-MM-dd'T'HH:mm:ss.SSSZ.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜