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.