1

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

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

我写了一些代码让你们清楚地理解问题,有类(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 实例提供了正确数量的毫秒,而在 show 方法中,它们打印所有相同的毫秒...

你有什么主意吗?我的印象是这是一个非常新的错误,但我无法弄清楚我做错了什么

4

2 回答 2

5

您正在为所有容器重用同一个日历实例(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++;
}
于 2012-05-08T13:01:59.467 回答
0

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

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

那么您将不会有重新使用日历实例的问题。正如我认为的那样,这将更有效率。

于 2012-05-08T13:04:05.143 回答