Bin java time stamp by hour/half hour based on date

时间:2015-09-01 21:33:06

标签: java datetime hashmap

I have a hashmap, which has keys as date_site, example Key: 2015-08-24_Demo-Site, Value:08:30, and then keys which are in sorted order by time. I want the values binned by time interval within an hour as one report and half hour as another.

For example,

Key: 2015-08-24_cimp, Value:16:13, 16:20, 16:34, 18:49, 18:57, 22:33

Should be a datapoint in the hour report as:

[2015-08-24, "cimp", 16-17, 3], [2015-08-24, "cimp", 18-19, 2], [2015-08-24, "cimp", 21-22, 1]

I attempted to make a calendar date time and then checking if the instance is after that, but I am sort of stuck and was wondering if there was an easier way to bin from the hashmap.

I am calling the function while iterating over keys, values in my hashmap:

for (Map.Entry<String, String> entry : daySiteMap.entrySet()) {
       System.out.println("Key: " + entry.getKey() + ", Value:" + entry.getValue());
       compareTime(entry.getValue());
    }



    private static ArrayList<String> compareTimeHour(String time) {
    String[] times = time.split(",");
    ArrayList<String> datas = new ArrayList<String>();

    if(times.length == 1){
        Integer hour = Integer.parseInt(times[0].split(":")[0].trim());
        Integer hour1 = hour+1;
        datas.add(hour.toString() + "-" + hour1.toString());
        datas.add("1");
    }
    else{
        for (int i = 0; i < times.length; i++) {
            Calendar check = Calendar.getInstance();
            check.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[i].split(":")[0].trim()));
            check.set(Calendar.MINUTE, Integer.parseInt(times[i].split(":")[1].trim()));
            boolean checking = true;
            Integer startHour = 1;
            Integer startMin = 0;
            while(checking){
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, startHour);
                cal.set(Calendar.MINUTE, startMin);
                checking = Calendar.getInstance().before(check);
                startHour+=1;
                startMin+=30;
            }
        }
    }
    return datas;

}

1 个答案:

答案 0 :(得分:3)

The following is an example of how to do something like that:

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Map<Date, String> activityMap = new TreeMap<Date, String>();
activityMap.put(fmt.parse("2015-04-01 09:00:00"), "Show up at work");
activityMap.put(fmt.parse("2015-04-01 09:05:00"), "Get coffee");
activityMap.put(fmt.parse("2015-04-01 09:15:00"), "Read the news");
activityMap.put(fmt.parse("2015-04-01 10:02:00"), "Smoke break");
activityMap.put(fmt.parse("2015-04-01 10:38:00"), "Watercooler break");
activityMap.put(fmt.parse("2015-04-01 11:45:00"), "Lunch break");

Map<Date, List<String>> dateHourMap = new TreeMap<Date, List<String>>();
Calendar cal = Calendar.getInstance();
for (Map.Entry<Date, String> activity : activityMap.entrySet()) {
    cal.setTime(activity.getKey());
    int year   = cal.get(Calendar.YEAR);
    int month  = cal.get(Calendar.MONTH);
    int day    = cal.get(Calendar.DAY_OF_MONTH);
    int hour   = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    cal.clear();
    cal.set(year, month, day, hour, 0);
    Date dateHour = cal.getTime();
    List<String> list = dateHourMap.get(dateHour);
    if (list == null)
        dateHourMap.put(dateHour, list = new ArrayList<String>());
    list.add(String.format("%02d:%02d %s", minute, second, activity.getValue()));
}

fmt = new SimpleDateFormat("yyyy-MM-dd HH");
for (Map.Entry<Date, List<String>> entry : dateHourMap.entrySet())
    System.out.println(fmt.format(entry.getKey()) + ": " + entry.getValue());

Output

2015-04-01 09: [00:00 Show up at work, 05:00 Get coffee, 15:00 Read the news]
2015-04-01 10: [02:00 Smoke break, 38:00 Watercooler break]
2015-04-01 11: [45:00 Lunch break]