3

I want to extract the date/time pattern from a String.

For example, there are three Strings:

1: "current date: 01/05/2017."
2: "The start date was 15.11.2016."
3: "Christmas (24.12) was on saturday.

The first information that I need is the pattern:

1: "MM/dd/yyyy"
2: "dd.MM.yyyy"
3: "dd.MM"

The second information is the extracted date part:

1: "01/05/2017"
2: "15.11.2016"
3: "24.12"

... or the position of the first value of this result.

All possible patterns are known in advance.

I have no idea how I can realize this in an elegant and performant way.

Smoothi
  • 283
  • 1
  • 3
  • 15
  • 5
    Probably with regular expressions. – J Fabian Meier Jan 05 '17 at 14:56
  • 1
    Use regular expressions. – avilac Jan 05 '17 at 14:57
  • 1
    I second the regex approach. Just google date time regex – user489041 Jan 05 '17 at 14:57
  • Possible duplicate of [Regex to validate date format dd/mm/yyyy](http://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy) – xenteros Jan 05 '17 at 14:58
  • 4
    what logic can help you to decide whether 01/02/2000 is `1st Feb` or `2nd Jan`? – xenteros Jan 05 '17 at 14:59
  • @xenteros: In the origin string there is only month/day/year :) I think, it is no duplicate of another question. I didn't found this problem with an embedded date. With a (big) regex this could be possible, but how can I find the position? After I have found the position, maybe I can search the substring by another date. – Smoothi Jan 05 '17 at 15:11
  • @Smoothi `1: "MM/dd/yyyy" 2: "dd.MM.yyyy"` – xenteros Jan 05 '17 at 15:20
  • 1
    _After I have found the position, maybe I can search the substring by another date._ What in the world does that mean? Maybe you should try regex before over analyzing since you say the patterns are known in advance. – Christopher Schneider Jan 05 '17 at 15:20
  • @Smoothi I don't think the regex will be big at all :) – m0skit0 Jan 05 '17 at 15:23
  • @xenteros I meant the origin string has embedded date (or time) parts :) But the solution from Smurf can be helpful – Smoothi Jan 05 '17 at 15:30
  • @Chropher Schneider That means in the origin string there can be more than one date. So should check for patterns more than one time. Uninteresting for the solution. And all patterns I have to check are known, why I should not say that? – Smoothi Jan 05 '17 at 15:30
  • @m0skit0 One regex for one pattern is small, but there are a lot. :) – Smoothi Jan 05 '17 at 15:30
  • A lot? I can only see 3 in your question... – m0skit0 Jan 05 '17 at 15:32
  • @m0skit0 this was only an example for this question :) – Smoothi Jan 05 '17 at 15:33
  • 1
    FWIW, you should avoid parsing data from string representations, unless there is no alternative. To cite Josh Bloch: "In the absence of such methods [in a exception], programmers have been known to parse the string representation of an exception to ferret out additional information. This is extremely bad practice [...]. Classes seldom specify the details of their string representations, so string representations can differ from implementation to implementation and release to release. Therefore, code that parses the string representation of an exception is likely to be nonportable and fragile" – jpangamarca Jan 05 '17 at 15:35
  • @jpangamarca This is just the last solution. A better solution for my problem I'm looking for: http://stackoverflow.com/questions/41440624/placeholder-in-resource-bundles But if this is not possible, I want to try this parsing in a jsf renderer. The problem is, in the jsf renderer I only get the formatted value from properties file. – Smoothi Jan 05 '17 at 15:46

1 Answers1

2

How about something like this:

String regex = "(\\d{2}/\\d{2}/\\d{4})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("current date: 01/05/2017.");
if (matcher.find()) {
    start = matcher.start(); // start index of match
    end = matcher.end(); // end index of match
    result = matcher.group(1);
}

Now edit the regex based on the necessary pattern of the input.

Keep in mind, this does not check whether the given dates are valid.

Edit: Added two lines based on comments.