Search code examples
javasqlitedatedate-arithmeticceil

How to subtract two java.sql.Date dates?


Before insert into an SQLite database I check if a record for that day already exists (number of days is >0). For example difference between 2011-8-6 and 2011-8-5 is 1. How to do this in Java?

This could be done in database:

SELECT julianday('now') - julianday(Date(Date)) from VIDEO_HISTORY;

It gives me a decimal number: 3.3442346529103816. Now I have to decide in Java if the number is 3 or 4 days.

My app searches YouTube and writes daily views into database. User is able to schedule this daily search at 20:00 o'clock but could reschedule it to 10:00 o'clock. The program has to understand this as a difference of one day so I have to round up.

Is there some method for this or do I have to write it myself?


Solution

  • You may want to consult this tutorial. Moreover you should consider using Joda Time's Period and consult this answer on stackoverflow.