Given OP's new comment and requirements, the solution is to use a custom deserializer:
You would do something like this:
@JsonDeserialize(using = MyDateDeserializer.class)
class MyClassThatHasDateField {...}
See tutorial here: http://www.baeldung.com/jackson-deserialization
See an example here: Custom JSON Deserialization with Jackson
OLD ANSWER:
You can use Java's SimpleDateFormat
and either:
- Use a regex to choose the proper pattern
- Simply try them and catch (and ignore) the exception
Example:
String[] formats = { "dd MMM. yyyy", "dd MM yyyy" };
for (String format : formats)
{
try
{
return new SimpleDateFormat( format ).parse( theDateString );
}
catch (ParseException e) {}
}
OR
String[] formats = { "dd MMM. yyyy", "dd MM yyyy" };
String[] patterns = { "\\d+ [a-zA-Z]+\. \d{4}", "\\d+ [a-zA-Z]+ \d{4}" };
for ( int i = 0; i < patterns.length; i++ )
{
// Create a Pattern object
Pattern r = Pattern.compile(patterns[ i ] );
// Now create matcher object.
Matcher m = r.matcher( theDateString );
if (m.find( )) {
return new SimpleDateFormat( formats[ i ] ).parse( theDateString );
}
}