有没有人知道将根据时间间隔操纵数据的库?

时间:2011-07-30 21:50:44

标签: java time recommendation-engine

是否有人知道可以根据时间间隔操纵/组织数据的库?

例如:

  • 我有一个按日期/时间映射到对象的数据集合[可以是任何东西,无关数据列表,字符串,数字等]
  • 我希望Collection能够通用,以便它可以处理间隔搜索,组织,合并,交叉点,差异和间隔变化[即该集合将重新组织一个15分钟的间隔,间隔为1天]

我正在考虑写下其中一个,但我宁愿不重新发明轮子。

另外,最后的规定是,我希望它是用Java。

例如:

  

您有以下数据:

     
      
  • 1/1/11 1:01 - “鲍勃进入房间”
  •   
  • 1/1/11 12:01 - “杰瑞进了房间”
  •   
  • 1/1/11 1:31 - “莎莉进了房间”
  •   
  • 1/1/11 1:51 - “Jorge进入房间”
  •   
  • 1/1/11 2:01 - “Dilbert进入房间”
  •   
  • 1/2/11 1:01 - “鲍勃进入房间”
  •   
  • 1/3/11 12:01 - “杰瑞走进房间”
  •   
  • 1/2/11 1:31 - “莎莉进了房间”
  •   
  • 1/2/11 1:51 - “Jorge进入房间”
  •   
     

此处所述的所有企业[作为2值数据]将进入集合[ie]:add(Date,object)      但是在初始化时,将设置间隔。 [即15分钟]      因此,如果间隔是15分钟[并查询特定时间。它会产生:

     
      
  • 1/1/11 12:00-12:15      
        
    • “杰瑞走进房间”
    •   
  •   
  • 1/1/11 1:00-1:15
           - “鲍勃进入房间”
  •   
  • 1/1/11 1:15-1:30
  •   
  • 1/1/11 1:30-1:45

         
        
    • 1/1/11 1:31 - “莎莉进了房间”   ....
    •   
         

    如果你试图查询1/1/11 12:09,你会得到1/1/11 12:00-12:15的结果。   是的我知道有边缘情况,但这只是一个例子。

  •   

1 个答案:

答案 0 :(得分:4)

以下是TreeMap包装器的草图,基本上与您的示例相同:

public class CalendarMap<V> {

    private TreeMap<Calendar, V> map = new TreeMap<Calendar, V>();

    public void put(Calendar d, V v){
        map.put(d, v);
    }

    public void query(Calendar d, int intervalUnit, int intervalValue){
         DateFormat df = new SimpleDateFormat("MMM dd, yyyy");
         DateFormat tf = new SimpleDateFormat("HH:mm");

        // snap closest prior unit
        d.set(intervalUnit, (d.get(intervalUnit) / intervalValue)* intervalValue);
        Calendar next = new GregorianCalendar();
        next.setTime(d.getTime());
        next.add(intervalUnit, intervalValue);

        Calendar lastHit = null; // last hit

        while(d.before(map.lastKey())){
            SortedMap<Calendar, V> hits = map.subMap(d, true, next, false);
            if(!hits.isEmpty()){
                if(lastHit != null){
                    System.out.println(df.format(lastHit.getTime()) + " " + tf.format(lastHit.getTime()) + " - " + 
                            df.format(d.getTime()) + " " + tf.format(d.getTime()) + ": N/A");
                    lastHit = null;
                }
                System.out.println(df.format(d.getTime()) + " " + tf.format(d.getTime()) + "-" + tf.format(next.getTime()) + ":");
                for(Entry<Calendar, V> entry : hits.entrySet()){
                    System.out.println("  " + tf.format(entry.getKey().getTime()) + " - " + entry.getValue());
                }
            }else if(lastHit == null){
                lastHit = new GregorianCalendar();
                lastHit.setTime(d.getTime());
            }
            d.add(intervalUnit, intervalValue);
            next.add(intervalUnit, intervalValue);
        }
    }

    public static void main(String[] args){
        CalendarMap<String> map = new CalendarMap<String>();
        map.put(new GregorianCalendar(2011, 1, 1, 13, 1), "Bob entered the room");
        map.put(new GregorianCalendar(2011, 1, 1, 12, 1), "Jerry entered the room");
        map.put(new GregorianCalendar(2011, 1, 1, 13, 31), "Sally entered the room");
        map.put(new GregorianCalendar(2011, 1, 1, 14, 1), "Dilbert entered the room");
        map.put(new GregorianCalendar(2011, 1, 2, 13, 1), "Bob entered the room");
        map.put(new GregorianCalendar(2011, 1, 3, 12, 1), "Jerry entered the room");
        map.put(new GregorianCalendar(2011, 1, 2, 13, 31), "Sally entered the room");
        map.put(new GregorianCalendar(2011, 1, 2, 13, 51), "Jorge entered the room");

        map.query(new GregorianCalendar(2011, 1, 1, 12, 9), Calendar.MINUTE, 15);
    }

}

这会产生:

Feb 01, 2011 12:00-12:15:
  12:01 - Jerry entered the room
Feb 01, 2011 12:15 - Feb 01, 2011 13:00: N/A
Feb 01, 2011 13:00-13:15:
  13:01 - Bob entered the room
Feb 01, 2011 13:15 - Feb 01, 2011 13:30: N/A
Feb 01, 2011 13:30-13:45:
  13:31 - Sally entered the room
Feb 01, 2011 13:45 - Feb 01, 2011 14:00: N/A
Feb 01, 2011 14:00-14:15:
  14:01 - Dilbert entered the room
Feb 01, 2011 14:15 - Feb 02, 2011 13:00: N/A
Feb 02, 2011 13:00-13:15:
  13:01 - Bob entered the room
Feb 02, 2011 13:15 - Feb 02, 2011 13:30: N/A
Feb 02, 2011 13:30-13:45:
  13:31 - Sally entered the room
Feb 02, 2011 13:45-14:00:
  13:51 - Jorge entered the room
Feb 02, 2011 14:00 - Feb 03, 2011 12:00: N/A
Feb 03, 2011 12:00-12:15:
  12:01 - Jerry entered the room

假设您执行此操作:

map.query(new GregorianCalendar(2011, 1, 1, 12, 9), Calendar.HOUR, 1);

然后输出是:

Feb 01, 2011 12:09-13:09:
  13:01 - Bob entered the room
Feb 01, 2011 13:09-14:09:
  13:31 - Sally entered the room
  14:01 - Dilbert entered the room
Feb 01, 2011 14:09 - Feb 02, 2011 12:09: N/A
Feb 02, 2011 12:09-13:09:
  13:01 - Bob entered the room
Feb 02, 2011 13:09-14:09:
  13:31 - Sally entered the room
  13:51 - Jorge entered the room
Feb 02, 2011 14:09 - Feb 03, 2011 11:09: N/A
Feb 03, 2011 11:09-12:09:
  12:01 - Jerry entered the room