1

Is anybody aware of a third-party package that directly finds portions of an input string that match a Java DateFormat? In other words, that converts a DateFormat to a regular expression?

E.g., it might convert;

DateFromat df = new SimpleDateFormat("MM/dd/yyyy")

to something like "(0[1-9]|1[0-2])\\/(0[1-9]|1[0-9]|2[0-9]|3[01])\\/([0-9]{4})" so that you would have seamless methods available like (fantasy code):

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

DateMatcher dm = df.dateMatcher(inputString);

while (dfm.findDate()) {
#dostuff
}; // or 

if (dfm.matchesDate()) #dosomething
anubhava
  • 761,203
  • 64
  • 569
  • 643
PatentWookiee
  • 187
  • 2
  • 17

2 Answers2

2

SimpleDateFormat#toPattern gets the pattern String of your SimpleDateFormat instance! So instantiate a SimpleDateFormat with the constructor of your choice and use toPattern to extract the pattern String.

Daniel
  • 855
  • 10
  • 21
1

yes this magic parser code is available in joda-time package. Check the implementation of DateTimeParser there.

Check classes InternalParserDateTimeParser, DateTimeParserBucket to understand how jodatime extracts date as given pattern from the input line.

Puneet Jaiswal
  • 313
  • 4
  • 9