• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about subtract dates

 
mohamed abdo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i subtract two dates from each other,i need the result with the days count between them , (date - date = xxx day)
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to a more suitable forum.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Calendar#DAY_OF_YEAR and Calendar#get()
 
mohamed abdo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sir , i found it
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the naive approach, which is broken because of daylight savings - there are two days a year that are not 24 hours.

The right approach indeed uses Calendar.get() in combination with both Calendar.DAY_OF_YEAR and Calendar.YEAR. In short:
- subtract the day of year of the first date from the day of year of the second date
- add the number of days for each year in difference as well; use Calendar.getActualMaximum(Calendar.DAY_OF_YEAR) to determine how many days a year has.

To make it easier, you can swap the operands if the first date is larger than the second:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic