You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Weird bug - Custom date parser for parsing dates with zero prefixes #33
For a specific usecase, I needed to parse a date of format yyyy-mm-dd where each component might be prefixed by a zero
I am trying to write a custom parser which is able to parse this
for eg
02022-012-009 should be parsed as 2022-12-09
This is my code
import com.github.sisyphsu.dateparser.DateParser;
public class DateUtilsApplication {
public static void main(String[] args) {
DateParser dateParser = DateParser.newBuilder()
.addRule("0?(?<year>\\d{4})\\W{1}0?(?<month>\\d{1,2})\\W{1}0?(?<day>\\d{1,2})")
.build();;
//example 1 (no zeros)
System.out.println(dateParser.parseDateTime("2022-12-09").toLocalDate());
//prints "2022-12-09"
//example 2 (year, month and date have zero prefix)
System.out.println(dateParser.parseDateTime("02022-012-009").toLocalDate());
//prints "2022-12-09"
//example 3 (month and date have zero prefix)
System.out.println(dateParser.parseDateTime("2022-012-009").toLocalDate());
//prints "2022-12-09"
//example 4 (date has zero prefix)
System.out.println(dateParser.parseDateTime("2022-12-009").toLocalDate());
//expected "2022-12-09", but errors out
}
}
All examples use the same dateparser but the fourth errors out.
very weird because I have given 0? for all 3 components.
addRule appends to a builder already populated with the default rules. One default year-month-day rule can match a prefix ending in -00 from 2022-12-009; its final [^\d]? is optional, so it does not require a numeric boundary. The parser commits the selected rule's fields before checking that the complete input was consumed. That makes rule selection worth checking, rather than adding more optional zeros: log matcher.re() and matcher.end() to confirm which rule wins.
For this constrained field, a deterministic workaround is to full-match your expression with a standalone java.util.regex.Matcher, convert the three captures with LocalDate.of, then use the resulting canonical ISO date. That validates leap days and rejects impossible dates without competing partial rules. Keep all four examples as regressions, plus an invalid date followed by a valid parse on the same parser instance.
Activity
Alox1d commentedon Oct 15, 2026
addRuleappends to a builder already populated with the default rules. One default year-month-day rule can match a prefix ending in-00from2022-12-009; its final[^\d]?is optional, so it does not require a numeric boundary. The parser commits the selected rule's fields before checking that the complete input was consumed. That makes rule selection worth checking, rather than adding more optional zeros: logmatcher.re()andmatcher.end()to confirm which rule wins.For this constrained field, a deterministic workaround is to full-match your expression with a standalone
java.util.regex.Matcher, convert the three captures withLocalDate.of, then use the resulting canonical ISO date. That validates leap days and rejects impossible dates without competing partial rules. Keep all four examples as regressions, plus an invalid date followed by a valid parse on the same parser instance.I maintain DateParser Kotlin; its explicit-date validation is an MIT reference for validating the complete calendar value. I have not run this reproduction.