-3

I'm writing an application where I need to read previous lower and next higher date. For that I used reference from here. But Map.lowerKey(Date) every time return same request..(Date) object.

Sample Code:

Calendar c = Calendar.getInstance();

        TreeMap<Date, String> treemap = new TreeMap<>();

        for (int i = 0; i < 5; i++) {
            treemap.put(c.getTime(), (i + 1) + "");

            if(i == 3)
                c.add(Calendar.DATE, 2);  // number of days to add
            else
                c.add(Calendar.DATE, 1);  // number of days to 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 it 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 is fine but lowerKey returning same request object.

Anything I'm missing here ?

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
CoDe
  • 11,056
  • 14
  • 90
  • 197

2 Answers2

0

If you change your output to 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 as millisecond time), you'll receive an output like that:

C_Time : 1431779062507, L_Time: 1431779062492, G_Time: 1431865462492
                   ^^^                    ^^^

L_Time, which was put into the map, is different from C_Time, which was created after, in a few milliseconds, and that's why those two dates are treated as different: L_Time < C_Time. So your code is working absolutely correctly.

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • got it ..#makesense...so as my requirement is there any way to get previous/next date(By Day) for given Date index ? – CoDe May 15 '15 at 12:39
  • @Shubh use `LocalDate` for this: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html – Alex Salauyou May 15 '15 at 12:41
0

Ahha :) instead to go for LocalDate or JodaTime following trick work:

  public Calendar getCleanDate(Calendar targetDate) {
        Calendar newDate = (Calendar) targetDate.clone();
        newDate.setLenient(false);
        newDate.set(Calendar.HOUR_OF_DAY, 0);
        newDate.set(Calendar.MINUTE,0);
        newDate.set(Calendar.SECOND,0);
        newDate.set(Calendar.MILLISECOND,0);
        return newDate;
    }

Thanks Sasha and Jean to point the issue.

CoDe
  • 11,056
  • 14
  • 90
  • 197
  • Using `Date()` with zeroed hours, minutes and seconds is workaround, and it is wrong because of timezone issues (two dates having the same day but defined in different timezones still are different). `LocalDate` class is designed specially to store date information independent on locale and/or timezone. – Alex Salauyou May 15 '15 at 15:43
  • Not getting..could u please explain with any example ! – CoDe May 18 '15 at 08:44
  • http://stackoverflow.com/questions/5050170/java-getting-date-without-time see answers and discussions – Alex Salauyou May 18 '15 at 11:02