Can you split a String by a String string with a structure?

0

I collect the data from an xml that has 200 elements and within those elements is the date of each one, being its structure like this:

2018-05-04 00:00:00.0 2018-02-02 12:00:00.0  

But for example there are others who instead of having only two dates have 4

(2018-05-04 00:00:00.0 2018-02-02 12:00:00.0 
2018-05-04 00:00:00.0 2018-02-02 12:00:00.0) 

And I do not know how to do to collect only the dates in String depending on how many you have the specific element.

    
asked by Adr 08.05.2018 в 12:25
source

3 answers

0

You can try to extract it by means of a regular expression, in the following way:

String date = "2018-05-04 00:00:00.0 2018-02-02 12:00:00.0";
String newDate = "";

Pattern p = Pattern.compile("[0-9]*-[0-9]*-[0-9]*");
Matcher m = p.matcher(date);

while (m.find())
{
    // Cada fecha de forma individual:
    System.out.println(m.group(0));

    // Fechas concatenadas en una misma cadena:
    newDate += m.group(0) + " "; 
}

System.out.println(newDate);

The output of the concatenation would be:

2018-05-04 2018-02-02

You are worth as much for chains with 2, 4 or any number of dates, since you are extracting the next match until you run out of any. You should use Pattern and Matcher .

    
answered by 08.05.2018 / 12:53
source
2

You can do this by passing a split parameter to a blank space ("")

String str = "2018-05-04 00:00:00.0 2018-02-02 12:00:00.0";
String[] resp = str.split(" ");
/**
* resp[0] = 2018-05-04
* resp[1] = 00:00:00.0
* resp[2] = 2018-02-02
* resp[3] = 12:00:00.0
*/

Then you can concatenate two strings:

String part1 = resp[0]
String part2 = resp[1]
String value = part1 + " " + part2; // 2018-05-04 00:00:00.0

If it is necessary to only obtain all the dates in a String you can do it with a loop with even indexes.

String val = "2018-05-04 00:00:00.0 2018-02-02 12:00:00.0 2018-05-04 00:00:00.0 2018-02-02 12:00:00.0";
String resp[] = val.split(" ");
String print = "";

for (int i = 0; i < resp.length; i = i + 2) {
    print += resp[i] + " ";     
}

System.out.print(print); // 2018-05-04 2018-02-02 2018-05-04 2018-02-02 
    
answered by 08.05.2018 в 12:30
1

Since you never know how many dates there may be in the chain, then it would be convenient:

  • make value groups by every two blank spaces
  • With each of these groups of values create an object of type Date formatted as you want, and keep it inside an array.
  • At the end you will have an array with all the dates in the chain, which you can use for whatever you need.
  • It would be something like this:

    String dateString="2018-05-04 00:00:00.0 2018-02-02 12:00:00.0 2018-02-03 12:00:00.0 2018-02-05 12:00:00.0 2018-02-06 12:00:00.0";
    
    ArrayList<Date> dateList = new ArrayList<Date>();
    String[] stringArray = dateString.split("(?<!\G\S+)\s");
    
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    for (String dateParts : stringArray) {
        try {
            dateList.add(simpleDateFormat.parse(dateParts));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    for (Date date : dateList) {
        System.out.println(simpleDateFormat.format(date));
    }        
    

    In this case, given that the string I've tried is this:

      

    2018-05-04 00: 00: 00.0 2018-02-02 12: 00: 00.0 2018-02-03 12: 00: 00.0   2018-02-05 12: 00: 00.0 2018-02-06 12: 00: 00.0

    You will have an array of dates with these values:

    2018-05-04
    2018-02-02
    2018-02-03
    2018-02-05
    2018-02-06
    

    Regardless of the dates, the code will create an array with each of them.

    I hope it serves you.

        
    answered by 08.05.2018 в 12:58