1

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"
Kabilesh
  • 1,000
  • 6
  • 22
  • 47
  • Please provide a link to API document you are talking about. "yyyymmddhh24miss" is ambiguous. – Stephen C Nov 26 '20 at 10:56
  • That document cannot be shared here. It's an API by our company. I just need to parse a date which looks like "20180314110343" – Kabilesh Nov 26 '20 at 10:59
  • 1
    Look at [the API documentation](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html) for `SimpleDateFormat`. Your format string is wrong. Note that upper-case and lower-case is important. Note that `mm` means minutes, not months. You probably want `yyyyMMddHHmmss`. Note that `hh24` is invalid, and `mi` is also invalid. – Jesper Nov 26 '20 at 11:01
  • If you cannot share the API document, you need to explain **in words** precisely what the (supposed) format `"yyyymmddhh24miss"` means, because it is ambiguous. – Stephen C Nov 26 '20 at 11:02
  • As @YCF_L mentioned, it should be fault in the documentation. They have mentioned the format of date is "yyyymmddhh24miss". – Kabilesh Nov 26 '20 at 11:10
  • 1
    @Kabilesh: It may well not be a fault in the documentation - it may well be that you're assuming that documentation for one thing applies to `SimpleDateFormat`. You shouldn't assume that format strings are the same on every platform etc. – Jon Skeet Nov 26 '20 at 11:44
  • 1
    Also note that the accepted answer uses `DateTimeFormatter` and `LocalDateTime`, from the `java.time` package, instead of the obsolete `Date` and `SimpleDateFormat` classes. You should be using that too. [See here why the old API is not good](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api). – MC Emperor Nov 26 '20 at 11:47
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 26 '20 at 18:21

2 Answers2

2

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
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

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

Jabir
  • 2,776
  • 1
  • 22
  • 31