how to remove special characters and to get two dates from given string?
public class Remove {
public static void main(String[] args) {
String s="[From:14 02 1986,To:14 02 2016]";
System.out.println(s);
}
}
how to remove special characters and to get two dates from given string?
public class Remove {
public static void main(String[] args) {
String s="[From:14 02 1986,To:14 02 2016]";
System.out.println(s);
}
}
Here is a solution with regex:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Remove {
public static void main(String[] args) {
String s="[From:14 02 1986,To:14 02 2016]";
Pattern r = Pattern.compile("From:(\\d\\d \\d\\d \\d\\d\\d\\d),To:(\\d\\d \\d\\d \\d\\d\\d\\d)");
Matcher m = r.matcher(s);
if (m.find( )) {
System.out.println("From date : " + m.group(1) );
System.out.println("To date : " + m.group(2) );
}else {
System.out.println("NO MATCH");
}
}
}
You can use string.split() to get the dates;
String[] dates = s.split(",");
That will give you a string array ["From:14 02 1986", "To:14 02 2016"]
then clean the unnecesary text from splits;
for (int i = 0; i < dates .length; i++) {
dates[i] = dates[i].substring(dates[i].indexOf(":") + 1)
}
This will remove "From:" and "To:" from array, making it:["14 02 1986", "14 02 2016"]
, then cast them to DateTime using DateFormat;
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd MM yyyy");
for (String date: dates) {
println(formatter.parseDateTime(date))
}
Edit: just saw the square brackets at the beginning and end of your string, you can remove them in same manner.