Java – Jodatime is malformed, even if it looks correct

Jodatime is malformed, even if it looks correct… here is a solution to the problem.

Jodatime is malformed, even if it looks correct

I have this code

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss Z");
DateTime dateNow = dtf.parseDateTime(new DateTime().toString());
String registerDateStr = dateNow.toString();

But when I run it, it shows the malformed date:

java.lang.IllegalArgumentException: Invalid format: “2019-06-13T17:57:47.420+08:00” is malformed at “.420+08:00”

The date of formation seems correct to me? Am I having formatting issues?


You’re working too hard. You don’t need any explicit formatters at all.

    DateTime dateNow = new DateTime();
    String registerDateStr = dateNow.toString();


Output from the runtime just now:


You can also parse strings without a formatter if you want, but I see no reason for you to do so because you’ll only get a DateTime object, equal to the object you started:

    DateTime parsedBack = DateTime.parse(registerDateStr);


Do you know?

Note that Joda-Time is considered to be a largely “finished” project.
No major enhancements are planned. If using Java SE 8, please migrate
to java.time (JSR-310).

Quote from the Joda-Time home page

Related Problems and Solutions