java - List of timestamps overrides the previous timestamp -


i using arraylist store list of timestamps of past 5 weeks.

   i.e., if today 2014-06-09, want store     2014-06-02     2014-05-26     2014-05-19     2014-05-12           2014-05-05 

here code.

public class test {  public static void main(string ap[]) throws interruptedexception{ list<timestamp> ts = new arraylist<timestamp>(); timestamp t = new timestamp(new java.util.date().gettime()); timestamp temp = null;  for(int i=0;i<5;i++){     t.settime(t.gettime()-(7*24 * (long)60* (long)60) * (long)1000);     temp = t;     system.out.println(t);     ts.add(temp);     temp = null;         } } } 

but problem getting list of overrided values i.e., list contains elements last timestampi i.e 2014-05-05) can reply question?

the reason you're not getting "new" timestamps because keep overriding same 1 , adding list - end same object entered 5 times list , last value display in "all" items. don't need temp - create new timestamp object , add list:

    list<timestamp> ts = new arraylist<timestamp>();     timestamp t = new timestamp(new java.util.date().gettime());      for(int i=0;i<5;i++){         t.settime(t.gettime()-(7*24 * (long)60* (long)60) * (long)1000);         system.out.println(t);         ts.add(new timestamp(t.gettime()));     } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -