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?
Solution
You’re working too hard. You don’t need any explicit formatters at all.
DateTime dateNow = new DateTime();
String registerDateStr = dateNow.toString();
System.out.println(registerDateStr);
Output from the runtime just now:
2019-06-13T13:04:48.301+02:00
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);
System.out.println(parsedBack);
2019-06-13T13:04:48.301+02:00
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
tojava.time
(JSR-310).
Quote from the Joda-Time home page