3

I insert some data into Sqlite database and check before insert if record for this day allready exists. To check this I have to see if number of days are >0. For example difference (in days) between 2011-8-6 and 2011-8-5 is 1. How to do this in Java?

EDIT: As @RayToal mentioned this could be done in database, so I did on that way:

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

Only problem with this is that it gives me decimal number. For example: 3.3442346529103816 Now I have to decide inside Java if given number is 3 of 4 days. It code is for app that searches youtube for some term and writes statistical data about daily views into database. User is able to schedule job for example every day in 20:00 o'clock. But then he could decide to reschedule this job in 10:00 o'clock, so program has to understood this like difference is one day. So it's obvious that I have to round to first bigger number. Is there some method for this or I have to write myself?

EDIT2: According to links provided by @Michael-O this is best solution (using JodaTime):

DateTime start = new DateTime(new GregorianCalendar(2011, 8, 4).getTime());
DateTime end = new DateTime(new GregorianCalendar(2011, 8, 8).getTime());

int numberOfDays = Days.daysBetween(start.toDateMidnight(), end.toDateMidnight()).getDays();
System.out.println(numberOfDays);
skaffman
  • 398,947
  • 96
  • 818
  • 769
Иван Бишевац
  • 13,811
  • 21
  • 66
  • 93
  • 2
    The difference between 2011-8-6 and 2011-8-6 is actually 0. By the way, since the data is apparently stored in a DB, is there any reason why you don't use SQL for this? – BalusC Aug 05 '11 at 22:09
  • 2
    Since you should be using the database to compute this, I would say this question is a dup of http://stackoverflow.com/questions/289680/difference-between-2-dates-in-sqlite. If for some reason you _have_ to do the computation in Java, do say so. Then look into Joda-Time. :) – Ray Toal Aug 05 '11 at 22:14
  • @BalucC Sorry that was "lapsus calami", I edited this. – Иван Бишевац Aug 05 '11 at 22:15
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 19 '18 at 20:08

2 Answers2

3

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

Community
  • 1
  • 1
Michael-O
  • 18,123
  • 6
  • 55
  • 121
2

If you want the number of calendar days (a Period in java.time parlance), then use (in Java 8):

import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
ChronoUnit.DAYS.between(LocalDate.parse(from.toString()),LocalDate.parse(to.toString())

(This avoids a subtle bug in Java 8's java.sql.Date API)

If you want a Duration in days (physically elapsed time in 24-hour units), then use Marcelo's approach.

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58