So I was given a bunch of time slots in XML file:
2015-01-19 12:00:00 ==> 2015-01-19 13:00:00
2015-01-19 15:00:00 ==> 2015-01-19 17:00:00
2015-01-19 15:30:00 ==> 2015-01-19 18:30:00
2015-01-19 17:00:00 ==> 2015-01-19 18:00:00
2015-01-19 18:30:00 ==> 2015-01-19 19:00:00
2015-01-19 19:00:00 ==> 2015-01-20 03:00:00
My goal is to find all the gap with the provided time slot (in this case it should return the gap between 2015-01-19 13:00:00 and 2015-01-19 15:00:00)
I have tried to use a Treemap to sorted the start date and Set store the start date as the key and end date as the value. And store the start date and end date into two list. After that, I would compare both list to see if the current end date is after the start date:
import java.util.Date;
import java.util.Map.Entry;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class Finding_Time_Gap {
public static void main(String[] args) throws Exception {
//formatter for changing the data type (String to Date) for calculation.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
HashMap<String, String> date = new HashMap<>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("data_java-2.xml"));
NodeList nodeList = document.getElementsByTagName("bar");
//using hashmap to store the start date and end date
for(int x=0,size= nodeList.getLength(); x<size; x++) {
String_end_date=nodeList.item(x).getAttributes().getNamedItem("enddate").getNodeValue();
String_start_date= nodeList.item(x).getAttributes().getNamedItem("startdate").getNodeValue();
//hashmap key is the start date of the duty of one employee and the value would be the end date of the employee.
date.put(String_start_date,String_end_date);
}
// changing the start date from string type to date type
//List<String> string_start_date_list = new ArrayList(); //adding the start date to the list named "key".
List<Date> start_date_list = new ArrayList<>();
List<Date> end_date_list = new ArrayList<>();
TreeMap<String, String> sorted = new TreeMap<>(date);
Set<Entry<String, String>> mappings = sorted.entrySet();
for (Entry<String, String> mapping : mappings) {
start_date_list.add(formatter.parse(mapping.getKey()));
end_date_list.add(formatter.parse(mapping.getValue()));
}
for (int i = 1; i<start_date_list.size();i++) {
if (start_date_list.get(i).after(end_date_list.get(i-1))) {
System.out.println("There is a time gap between: " + end_date_list.get(i-1) + " ----- " +start_date_list.get(i));
}
}
}
}
However, the output is not correct.
If the given time intervals grow like this:
2015-01-19 12:00:00 ==> 2015-01-19 23:59:59
2015-01-19 12:00:00 ==> 2015-01-19 13:00:00
2015-01-19 15:00:00 ==> 2015-01-19 17:00:00
2015-01-19 15:30:00 ==> 2015-01-19 18:30:00
2015-01-19 17:00:00 ==> 2015-01-19 18:00:00
2015-01-19 18:30:00 ==> 2015-01-19 19:00:00
2015-01-19 19:00:00 ==> 2015-01-20 03:00:00
Although the first time intervals (2015-01-19 12:00:00 ==> 2015-01-19 23:59:59) has covered whole day. I would still getting the results that there is a gap between 2015-01-19 13:00:00 and 2015-01-19 15:00:00, which is covered by the first time intervals.
May I ask how could I solve this problem?