I am using Joda to parse dates and have a format where leading zeros are not used, e.g.:
Mon Nov 20 14:40:36 2006
Mon Nov 6 14:40:36 2006
<
Here’s the modern answer, using java.time, the successor of Joda-Time.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM ppd HH:mm:ss uuuu", Locale.ENGLISH);
String[] stringsToParse = {
"Mon Nov 20 14:40:36 2006",
"Mon Nov 6 14:40:36 2006"
};
for (String dateTimeString : stringsToParse) {
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println(dateTime);
}
Output:
2006-11-20T14:40:36 2006-11-06T14:40:36
To DateTimeFormatter.ofPattern
format letter p
means padding with spaces on the left. pp
means padding to two position. It can be used for both formatting and — as here — parsing.
I know you asked about Joda-Time. The Joda-Time home page says:
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).