Extracting dates from text in Java

后端 未结 5 1840
旧时难觅i
旧时难觅i 2021-01-13 17:20

Is it possible to extract dates from a string in Java?

I have 500+ string with different data. In them, there can be:
\"... period from 08.23.2011 - 09.05.2011..

5条回答
  •  情话喂你
    2021-01-13 18:10

    I would use a simple regex to get "likely" dates out first, and then parse them more carefully (ideally with Joda Time, IMO). I'd start off with a regex of \b\d{2}\.\d{2}\.\d{4}\b (plus escaping for the Java string of course).

    (The \b bit matches a word boundary, so 12345.45.12345 won't match.)

    You can make your regex more selective, of course, but it would be very hard to make it do all the validation required (imagine trying to encode all the rules for leap years in a regex) - so if you're going to need to validate as you parse anyway, there's not a lot of point in making the regex complicated.

提交回复
热议问题