You could still use “specialized” parsers (as you expected) and bind them: For example, you still have DateHourMinSecParser (for yyyyMMddHHmmSS ), DateHourMinParser (for yyyyMMddHHmm ) and DateParser (for yyyyMMdd ) all of which same interface:
public interface GenericDateParser { Date parseDate(String input) throws IllegalArgumentException; }
eg.
public class DateHourMinSecParser implements GenericDateParser { ... public Date parseDate(String input) throws IllegalArgumentException { ... } }
but each of these classes would actually take another GenericDateParser parameter - the idea was that each parser would first try to parse the date if the parsing (or some internal checks, such as the length of the string), then pass it to the next parser in the chain until there are no more parsers in the chain (in this case, it will throw an exception, or one of the members in the chain will return a value):
public class DateHourMinSecParser implements GenericDateParser { private GenericDateParser chained; public DateHourMinSecParser(GenericDateParser chained) { this.chained = chained; } public Date parseDate(String input) throws IllegalArgumentException { if( !internalChecks() ) {
and you initialize them:
GenericDateParser p = new DateHourMinSecParser( new DateHourMinParser(new DateParser(null)) );
and then just use the top level:
Date d = p.parse( '20110126' );
source share