0

I want to get the day before yesterday with Calendar, this is the code I use.

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());

// What's the difference between the following three lines?
cal.add(Calendar.DAY_OF_MONTH, -2);
cal.add(Calendar.DAY_OF_WEEK, -2);
cal.add(Calendar.DAY_OF_YEAR, -2);

System.out.println(cal.getTime());

The first parameter of cal.add() should be a number, the number could be DAY_OF_MONTH, DAY_OF_WEEK, or DAY_OF_YEAR, but I'm sure which one I should use. I tried them all and it seemed that all of them worked very well. Could someone tell me what's the difference between those three lines I highlighted in the code? I didn't find a clear explanation about it in the Java doc.

Paolo Forgia
  • 6,202
  • 8
  • 42
  • 57
Searene
  • 22,811
  • 37
  • 122
  • 172

0 Answers0