Retrieve part of a string from a full text

Souvik

I hve a string variable which contains a text along with some dates in it. Now I would like to retrieve the date from the text. How I can do it.

String a ="I am ready at time -S 2019-06-16:00:00:00 and be there"

Now I would like to retrieve 2019-06-16:00:00:00 from there. Date format will always be in the same format but I need to retrieve the date only from the text.

Tim Biegeleisen

Try using a regex matcher with the pattern:

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

Sample code:

String a = "I am ready at time -S 2019-06-16:00:00:00 and be there";
String pattern = "\\d{4}-\\d{2}-\\d{2}:\\d{2}:\\d{2}:\\d{2}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(a);
while (m.find()) {
     System.out.println("found a timestamp: " + m.group(0));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related