Java 一段时间

Java 一段时间,java,jodatime,Java,Jodatime,我正在为我的电子邮件制作一张信息图表。 它进行得相当顺利,但事情比它们更令人困惑,例如,如果我想获得某人在一个月内给我发送的电子邮件数量,那么我使用以下方法: for (Person p : AL_persons) { for (int y = firstYear; y <= lastYear; y++) { for (int m = 1; m <= 12; m++) { ArrayList<Email> emailThatMonth = new

我正在为我的电子邮件制作一张信息图表。 它进行得相当顺利,但事情比它们更令人困惑,例如,如果我想获得某人在一个月内给我发送的电子邮件数量,那么我使用以下方法:

for (Person p : AL_persons) {
  for (int y = firstYear; y <= lastYear; y++) {
    for (int m = 1; m <= 12; m++) {
      ArrayList<Email> emailThatMonth = new ArrayList<Email>();
      for (Email e : p.emails) {
        if (e.date.year().get() == y
          && e.date.monthOfYear().get() == m) {
          emailThatMonth.add(e);
        }
      }
      if (emailThatMonth.size() > maxEmailsMonth)
        maxEmailsMonth = emailThatMonth.size();
    }
  }
}
for(人员p:AL_人员){

对于(int y=firstYear;y当前的第一部分查看每个人的所有电子邮件12*numyears次。您可以简化为只查看一次,方法是查找每个电子邮件上的月份并随时间递增不同的计数器。我没有测试此代码,因为我不确定您的类结构中的某些细节

HashMap<Person,HashMap<String,Integer>> hm = new HashMap<Person,HashMap<String,Integer>>();  
for (Person p : all_persons) 
{
  HashMap<String,Integer> tempMap = new HashMap<String,Integer>();
  for (Email e : p.emails) 
  {
    String tag = e.date.year().get() + "-" + e.date.monthOfYear().get();
    int lastvalue = tempMap.get(tag); 
    tempMap.put(tag, lastvalue+1);
  }
  hm.put(p,tempMap);
}
HashMap hm=newhashmap();
对于(人员p:所有人员)
{
HashMap tempMap=新的HashMap();
电子邮件(e:p.emails)
{
字符串标记=e.date.year().get()+“-”+e.date.monthOfYear().get();
int lastvalue=tempMap.get(标记);
tempMap.put(标记,lastvalue+1);
}
hm.put(p,tempMap);
}
因此,对于第二部分(搜索给定月份的所有电子邮件),我们采用大致相同的方法。迭代一次,将所有匹配项添加到新列表中

String targetMonth = "2012-01";//or whatever month you're looking for
ArrayList<Email> emailThatMonth = new ArrayList<Email>();
for (Email e: emails)
{
    String tag = e.date.year().get() + "-" + e.date.monthOfYear().get();
    if (tag.compareToIgnoreCase(targetMonth) == 0)
      emailThatMonth.add(e);      
}
String targetMonth=“2012-01”//或您想要的任何月份
ArrayList emailThatMonth=新建ArrayList();
电子邮件(e:emails)
{
字符串标记=e.date.year().get()+“-”+e.date.monthOfYear().get();
如果(标记比较信号案例(targetMonth)==0)
在当月发送电子邮件。添加(e);
}

那么…你想得到什么信息?每个月的电子邮件数量?每个月收到的电子邮件最多?看起来你最终写了很多变量,这些变量在超出范围之前从未被读取过。我有一个arrayList persons,一个人持有一个arrayList,其中包含了该人的所有电子邮件。我想得到tot每个人在一个月内发送所有电子邮件。我还有一个arrayList,其中保存了所有人的所有电子邮件,因此我希望在那里获得一个月内的所有电子邮件(所有人)。你能解释一下我的冒险吗?我还注意到我使用了localDate,这对我来说是一个非常糟糕的主意,因为我可以在同一天收到多封电子邮件,localDate没有小时等。但我可以将其改为dateTime。在第一封邮件中,你会多次查看每个人的电子邮件列表,4个嵌套循环。每个循环都会通过son的列表1次,2个嵌套循环。它还提供历史记录,在您的示例中,唯一不超出范围的变量是1个月的最大值,而您说您需要每个月的数字。
HashMap<Person,HashMap<String,Integer>> hm = new HashMap<Person,HashMap<String,Integer>>();  
for (Person p : all_persons) 
{
  HashMap<String,Integer> tempMap = new HashMap<String,Integer>();
  for (Email e : p.emails) 
  {
    String tag = e.date.year().get() + "-" + e.date.monthOfYear().get();
    int lastvalue = tempMap.get(tag); 
    tempMap.put(tag, lastvalue+1);
  }
  hm.put(p,tempMap);
}
String targetMonth = "2012-01";//or whatever month you're looking for
ArrayList<Email> emailThatMonth = new ArrayList<Email>();
for (Email e: emails)
{
    String tag = e.date.year().get() + "-" + e.date.monthOfYear().get();
    if (tag.compareToIgnoreCase(targetMonth) == 0)
      emailThatMonth.add(e);      
}