这是一个包装器的草图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);
}
}
这会产生:
2011年2月1日12:00-12:15:
12:01 - 杰瑞进入房间
2011 年 2 月 1 日 12:15 - 2011 年 2 月 1 日 13:00:不适用
2011年2月1日13:00-13:15:
13:01 - Bob 进入房间
2011 年 2 月 1 日 13:15 - 2011 年 2 月 1 日 13:30:不适用
2011年2月1日13:30-13:45:
13:31 - 莎莉进入房间
2011 年 2 月 1 日 13:45 - 2011 年 2 月 1 日 14:00:不适用
2011年2月1日14:00-14:15:
14:01 - 呆伯特进入房间
2011 年 2 月 1 日 14:15 - 2011 年 2 月 2 日 13:00:不适用
2011年2月2日13:00-13:15:
13:01 - Bob 进入房间
2011 年 2 月 2 日 13:15 - 2011 年 2 月 2 日 13:30:不适用
2011年2月2日13:30-13:45:
13:31 - 莎莉进入房间
2011年2月2日13:45-14:00:
13:51 - 豪尔赫进入房间
2011 年 2 月 2 日 14:00 - 2011 年 2 月 3 日 12:00:不适用
2011年2月3日12:00-12:15:
12:01 - 杰瑞进入房间
假设您执行以下操作:
map.query(new GregorianCalendar(2011, 1, 1, 12, 9), Calendar.HOUR, 1);
然后输出是:
2011年2月1日12:09-13:09:
13:01 - Bob 进入房间
2011年2月1日13:09-14:09:
13:31 - 莎莉进入房间
14:01 - 呆伯特进入房间
2011 年 2 月 1 日 14:09 - 2011 年 2 月 2 日 12:09:不适用
2011年2月2日12:09-13:09:
13:01 - Bob 进入房间
2011年2月2日13:09-14:09:
13:31 - 莎莉进入房间
13:51 - 豪尔赫进入房间
2011 年 2 月 2 日 14:09 - 2011 年 2 月 3 日 11:09:不适用
2011年2月3日11:09-12:09:
12:01 - 杰瑞进入房间