在while循环中出现Calendar问题

时间:2012-05-08 12:58:07

标签: java calendar arraylist while-loop

我正在开发这个时间容器,它基本上包含一个名为week的类的实例,它由2个Calendar实例(开始和结束)和许多其他管理信息组成(现在不相关)

问题是我在while循环中创建了这些Week实例,并且我看到在创建周实例期间(在while循环中打印日历毫秒),millis很好,但如果我检查内容周列表,他们不是!日历的所有实例(开始和结束)包含所有周的相同值。

我写了一些代码让你们清楚地理解这个问题,有类(Container和BiggerContainer):

public class Container
{
  private static final long serialVersionUID = 1L;

  private Calendar          start;

  private Calendar          end;

  public Container(Calendar start, Calendar end)
  {

    this.start = start;
    this.end = end;

  }

  public Calendar getStart()
  {
    return start;
  }

  public Calendar getEnd()
  {
    return end;
  }
}

Bigger container:

    public class BiggerContainer
{
  private static final long    serialVersionUID = 1L;

  private ArrayList<Container> containerList    = new ArrayList<Container>();

  public void init()
  {

    int i = 0;

    long max = 604800000;

    long nu = Calendar.getInstance().getTimeInMillis();

    Calendar startC = Calendar.getInstance();

    Calendar endC = Calendar.getInstance();

    while (i <= 5)
    {

      startC.setTimeInMillis(nu);
      endC.setTimeInMillis(nu + max);
      System.out.println("At creation time: " + startC.getTimeInMillis() + " / " + endC.getTimeInMillis());
      containerList.add(new Container(startC, endC));

      nu += max;
      i++;
    }
  }

  public void show()
  {

    for (Container c : containerList)
    {

      System.out.println("Start millis: " + c.getStart().getTimeInMillis() + " end millis "
          + c.getEnd().getTimeInMillis());
    }
  }

  /**
   * TEST
   * 
   * @param args
   */
  public static void main(String[] args)
  {

    BiggerContainer bc = new BiggerContainer();

    bc.init();

    bc.show();
  }

}

输出:

At creation time: 1336480993036 / 1337085793036
At creation time: 1337085793036 / 1337690593036
At creation time: 1337690593036 / 1338295393036
At creation time: 1338295393036 / 1338900193036
At creation time: 1338900193036 / 1339504993036
At creation time: 1339504993036 / 1340109793036


Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036
Start millis: 1339504993036 end millis 1340109793036

正如您在init()方法中创建Container实例期间所看到的那样,Calendar实例提供了正确的millis数量,而在show方法中,它们打印所有相同的millis ...

你知道吗? 我的印象是它是一个非常新的错误,但我无法弄清楚我在哪里做错了

2 个答案:

答案 0 :(得分:5)

您正在为所有容器重复使用相同的Calendar(startC和endC)实例。在构造函数中创建日历的“克隆”/“副本”,或在循环中创建日历实例。

  public Container(Calendar start, Calendar end)
  {

    this.start = (Calendar) start.clone();
    this.end = (Calendar) end.clone();
  }

while (i <= 5)
{
  Calendar startC = Calendar.getInstance();
  Calendar endC = Calendar.getInstance();
  startC.setTimeInMillis(nu);
  endC.setTimeInMillis(nu + max);
  System.out.println("At creation time: " + startC.getTimeInMillis() 
          + " / " + endC.getTimeInMillis());
  containerList.add(new Container(startC, endC));

  nu += max;
  i++;
}

答案 1 :(得分:0)

您可以将时间戳存储在Container对象中,如

containerList.add(new Container(startC.getTime(), endC.getTime()));

那么重新使用日历实例就不会有问题了。而且我认为这会更有效率。