I have a java Multimap that contains an identifier mapped to a start date and an end date.
我有一个java Multimap,其中包含映射到开始日期和结束日期的标识符。
SetMultiMap<String,List<Date>> mymap = LinkedHashMultimap.create();
I am using this map in another method, where I want to retrieve all the keys whose end date is less than 1 week ago.
我在另一种方法中使用此映射,我想要检索结束日期小于1周前的所有键。
I tried this:
我试过这个:
DateTime lastWeek_joda = new DateTime().minusDays(7);
Date end_date = lastWeek_joda.toDate();
now i iterate as follows:
现在我迭代如下:
for (Map.Entry<String,List<date>> entry : mymap.entries())
String key = entry.getKey();
List<Date> value = entry.getValue();
if (end_date.equals(value.get(1))) {
key_set.add(key);
}
}
This doesnt return me the expected result? Can this be done any easier/different? Thanks in advance.
这不会给我带来预期的结果吗?这可以更容易/不同吗?提前致谢。
2 个解决方案
#1
2
You are checking for dates that exactly equal the week-prior-date. Instead, use compareTo and check if the week-prior-date is greater than (later than) the current value.
您正在检查与周前日期完全相同的日期。而是使用compareTo并检查周前日期是否大于(晚于)当前值。
for (Map.Entry<String,List<date>> entry : mymap.entries())
String key = entry.getKey();
List<Date> value = entry.getValue();
if (end_date.compareTo(value.get(1)) > 0) {
key_set.add(key);
}
}
#2
1
for (Map.Entry<String,List<date>> entry : mymap.entries())
String key = entry.getKey();
List<Date> value = entry.getValue();
if (checkDateRange(value.get(1))) {
key_set.add(key);
}
}
public boolean checkDateRange(Date tDate) {
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
int i = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek();
c.add(Calendar.DATE, -i - 7);
Date start = c.getTime();
c.add(Calendar.DATE, 6);
Date end = c.getTime();
//your logic goes here
if(start<=tDate<=end){
return true;
}
return false;
}
#1
2
You are checking for dates that exactly equal the week-prior-date. Instead, use compareTo and check if the week-prior-date is greater than (later than) the current value.
您正在检查与周前日期完全相同的日期。而是使用compareTo并检查周前日期是否大于(晚于)当前值。
for (Map.Entry<String,List<date>> entry : mymap.entries())
String key = entry.getKey();
List<Date> value = entry.getValue();
if (end_date.compareTo(value.get(1)) > 0) {
key_set.add(key);
}
}
#2
1
for (Map.Entry<String,List<date>> entry : mymap.entries())
String key = entry.getKey();
List<Date> value = entry.getValue();
if (checkDateRange(value.get(1))) {
key_set.add(key);
}
}
public boolean checkDateRange(Date tDate) {
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
int i = c.get(Calendar.DAY_OF_WEEK) - c.getFirstDayOfWeek();
c.add(Calendar.DATE, -i - 7);
Date start = c.getTime();
c.add(Calendar.DATE, 6);
Date end = c.getTime();
//your logic goes here
if(start<=tDate<=end){
return true;
}
return false;
}