1

how can I select date from text in java? for example if I have dates in format: 2007-01-12abcd, absc2008-01-31 and I need to have dates in format: 2007-01-12, 2008-01-31 (without text). I used matcher in my code but it is not working.

for (int i=0; i < list.size(); i++) {
    Pattern compiledPattern = Pattern.compile("((?:19|20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])",  Pattern.CASE_INSENSITIVE);
    Matcher matcher = compiledPattern.matcher(list.get(i));
    if (matcher.find() == true) {
        new_list.add(list.get(i));  
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360

2 Answers2

2

I would keep things simple and just search on the following regex pattern:

\d{4}-\d{2}-\d{2}

It is fairly unlikely that anything which is not a date in your text already would match to this pattern.

Sample code:

String input = "2007-01-12abcd, absc2008-01-31";
String pattern = "\\d{4}-\\d{2}-\\d{2}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    System.out.println(m.group(0));
}

This prints:

2007-01-12
2008-01-31

By the way, your regex pattern can't be completely correct anyway, because it doesn't handle odd edge cases such as leap years, where February has 29 instead of 28 days.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

well i havent made a code but i think i might help you. First of all I presuppose that the format of the date in the string is already the right way(the order of the numbers is right and there are commas between the dates). Go through the string with a for-each for each character. If the current character(char) is a proper letter like a, b or c then you donw add it to the final string. If not you do add it. If the character is a comma you have to add this string to the list. The same should happen if it is the last character. This might not be the best way to do that but i am very sure it should work