Kabilesh
Kabilesh

Reputation: 1012

Getting java.text.ParseException: Unparseable date: "20201116135151"

I am trying to parse a date returned from an API. An example date looks like "20180314110343". The API document says the date format is yyyymmddhh24miss. However, when I try to parse using this format, I get the below error.

java.lang.IllegalArgumentException: Illegal pattern character 'i'

I tried parsing using the following code. But, I am getting a java.text.ParseException. What is the proper way to parse this date?

Code

DateFormat format = new SimpleDateFormat("yyyymmddhh24mmss");
String.valueOf(format.parse(dateVariable)

Error

java.text.ParseException: Unparseable date: "20201116135151"

Upvotes: 1

Views: 222

Answers (2)

Jabir
Jabir

Reputation: 2876

I was able to parse your date with following.

 String dateVariable = "20201116135151";
 DateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss" );

Parsed date is

Thu Jan 16 13:51:51 AST 2020

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60045

The format pattern is not a correct one for your date, or maybe there are a typo in the Api docs, but to parse the date you post it, you can use yyyyMMddHHmmss or uuuuMMddHHmmss:

LocalDateTime ldt = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("uuuuMMddHHmmss"));

Output

2018-03-14T11:03:43

Upvotes: 2

Related Questions