java - Speeding up date pattern matching -


i writing simple code tries deduce whether or not specific string java date and, if yes, identify format (pattern).

obviously, because there many possible date formats, establishing 1 applicable string requires successive pattern matching, time , cpu-consuming, given input string can have other values, too.

so, have ended doing, string variable called input, like

string datepattern;  if (islikedate(input)) {     datepattern = matchanyofthepredefineddatepatterns(input); } 

where islike... method rejects obvious non-date strings , match... method goes on 40-50 predefined patterns, trying construct valid simpledateformat object. constructor throws exception if input string not valid date pattern examined each time.

the exception handling slows things down dramatically, there seems no avoiding it. apache commons date packages exhibit similar performance.

is there faster way of implementing date pattern matching?

depending on complexity of patterns, might want match each potential pattern regex (or hand-written code) before trying parse date. example, if pattern "yyyymmddthh:mm:ss" check length, position of t, position of colons, , else digit before passing on date parsing code.

this level of pattern matching can liberal - it's trying rule out definite infringements of pattern. important thing doesn't reject values valid.

the downside pattern does match, you're doing work twice - may still balanced reducing number of exceptions throw.

edit: clarify, you're testing whether looks match any of patterns, , testing all of them. i'm suggesting have regex each pattern, , try parsing against patterns have matched corresponding regex.

i'd suggest trying joda time - not better api, patterns thread-safe, can reuse them. presumably you're creating new simpledateformat objects each time have parse.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -