@Test
public void splitTimeSlotsToArray() {
String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
// We already know how many times there are, each range (or slot)
// has two times specified in it. So it's the length of timeSlots times 2.
String[] times = new String[timeSlots.length*2];
for (int i = 0; i < timeSlots.length; i++) {
String timeSlotParts[] = timeSlots[i].split(" - ");
times[i*2] = timeSlotParts[0];
times[i*2 + 1] = timeSlotParts[1];
}
assertEquals(Arrays.asList(
"13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00"
), Arrays.asList(times));
}
// This is a more preferable option in terms of readability and
// idiomatics in Java, however it also uses Java collections which you
// may not be using in your class
@Test
public void splitTimeSlotsToList() {
String[] timeSlots = { "13:00:00 - 14:00:00", "15:00:00 - 16:00:00","17:00:00 - 18:00:00" };
Collection<String> times = new ArrayList<>();
// Go over each time slot
for (String timeSlot : timeSlots) {
// Go over each time in each time slot
for (String time : timeSlot.split(" - ")) {
// Add that time to the times collection
times.add(time);
}
}
// you can convert the Collection to an array too:
// String[] timesArray = times.toArray(new String[timeStamps.size()]);
assertEquals(Arrays.asList(
"13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00", "18:00:00"
), times);
}