-2

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);
  }
}
VPK
  • 3,010
  • 1
  • 28
  • 35
karthi
  • 3
  • 1
  • Is the width/format of your input date string always the same? – Tim Biegeleisen Feb 28 '17 at 06:15
  • 1
    I might start by splitting the string by `,` then each of the resulting strings by `:` – MadProgrammer Feb 28 '17 at 06:17
  • 1
    Welcome to Stack Overflow! We are a question-and-answer site, not a coders-for-hire service. Please explain what you have tried so far and why it hasn't worked. See: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) – Joe C Feb 28 '17 at 06:18
  • @MadProgrammer Or use a regex, or a series of substrings. Without further information, we don't know. – Tim Biegeleisen Feb 28 '17 at 06:18
  • @TimBiegeleisen I was going down the simplest route, but they are all viable options :P – MadProgrammer Feb 28 '17 at 06:19
  • Here is a regex pattern to parse: From:(\d\d) (\d\d) (\d\d\d\d),To:(\d\d) (\d\d) (\d\d\d\d) – betontalpfa Feb 28 '17 at 06:19
  • *"special characters"* - which are? – MadProgrammer Feb 28 '17 at 06:19
  • 1
    Sorry, this is not the way StackOverflow works. Questions of the form _"I want to do X, please give me tips and/or sample code"_ are considered off-topic. Please visit the [help] and read [ask], and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) – Jim Garrison Feb 28 '17 at 06:21

2 Answers2

0

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");
        }


    }

}
betontalpfa
  • 3,454
  • 1
  • 33
  • 65
0

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.

umutto
  • 7,460
  • 4
  • 43
  • 53