之前我大多不得不在特定时间段内的几天之间循环,并且我使用了这样的循环:
for(LocalDate iDate = gv.firstDate; iDate.isBefore(gv.lastDate); iDate = iDate.plusDays(1)) {
...
}
现在我有一个像这样的TreeMap
:
TreeMap<LocalDate, ArrayList<Email>> dates;
我想循环从 gv.firstDate
到 gv.lastDate
的所有月份,并获取该月内的所有电子邮件
。
有人知道使用 Joda-Time 执行此操作的好方法吗?
编辑:
将其与此结合起来会很棒,所以现在从日期 TreeMap 中获取电子邮件。
for(int y = 2004; y < 2011; y++) {
for(int m = 0; m < 12; m++) {
// get all of that month
}
}
最佳答案
您可以执行类似的操作:
for (Map.Entry<LocalDate, ArrayList<Email>> entry : dates) {
if (entry.getKey().isBefore(gv.firstDate())) {
continue;
}
if (entry.getKey().isAfter(gv.lastDate())) {
break;
}
// process the emails
processEmails(entry.getValue());
}
如果您可以自由使用 Google Guava,您可以执行以下操作:
Map<LocalDate, ArrayList<Email>> filteredDates = Maps.filterKeys(dates, new Predicate<LocalDate>() {
public boolean apply(LocalDate key) {
if (entry.getKey().isBefore(gv.firstDate())) {
return false;
}
if (entry.getKey().isAfter(gv.lastDate())) {
return false;
}
return true;
}
});
// process the emails
processEmails(filteredDates);
关于java - 如何使用 Joda-Time 循环月份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8750580/