java - Map.lowerKey(Date) not working -


i'm writing application need read previous lower , next higher date. used reference here. map.lowerkey(date) every time return same request..(date) object.

sample code:

calendar c = calendar.getinstance();          treemap<date, string> treemap = new treemap<>();          (int = 0; < 5; i++) {             treemap.put(c.gettime(), (i + 1) + "");              if(i == 3)                 c.add(calendar.date, 2);  // number of days add             else                 c.add(calendar.date, 1);  // number of days add         }          c = calendar.getinstance();          c.add(calendar.date, 1);         system.out.println("c_time : "+ c.gettime() + ", l_time: " + treemap.lowerkey(c.gettime()) + ", g_time: " + treemap.higherkey(c.gettime())); 

in response giving:

c_time : sat may 16 17:39:51 ist 2015, l_time: sat may 16 17:39:51 ist 2015, g_time: sun may 17 17:39:51 ist 2015

here higherkey value fine lowerkey returning same request object.

anything i'm missing here ?

if change output this:

 system.out.println("c_time : "+ c.gettime().gettime()        + ", l_time: " + treemap.lowerkey(c.gettime()).gettime()        + ", g_time: " + treemap.higherkey(c.gettime()).gettime()); 

(printing date millisecond time), you'll receive output that:

c_time : 1431779062507, l_time: 1431779062492, g_time: 1431865462492                    ^^^                    ^^^ 

l_time, put map, different c_time, created after, in few milliseconds, , that's why 2 dates treated different: l_time < c_time. code working absolutely correctly.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -