tags:

views:

44

answers:

4

I have string like dhgsf20101211124422asddas. this string contains date in yyyymmddhhmmss formate. How to get that date from long string like above. Thanks in advance

A: 

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html. Assuming it is java.

fastcodejava
I don't think `Scanner` "does" dates.
Carl Smotricz
A: 

regular expressions should work to grab the possible substrings - then some sanity logic to test the dates for any other validity

Randy
A: 

I'd use a regular expression. Assuming that it's always a 14 digit number and that there won't be any other exactly 14 digit long numbers in the string it would be easy to do.

There's quite a few tools to help you build regexs, I usually use http://www.redfernplace.com/software-projects/regex-builder/ which makes it easy even without much knowledge of how they work.

ho1
+1  A: 

I would use Java's DateFormat parsing. It allows you to march through a string using a ParsePosition object.

DateFormat format = new SimpleDateFormat("yyyymmddhhmmss");
ParsePosition pp = new ParsePosition(0);
for (int c = 0; c < text.length(); c++) {
    pp.setIndex(c); pp.setErrorIndex(-1);
    Date d = format.parse(text, pp);

    if (d != null) {
        // Handle parsed date

        // Only advance past the last parsed date if there was no error
        if (pp.getErrorIndex() < 0) c = pp.getIndex() - 1;
    }
}
J. Dimeo