1

I want to store the last time I inserted a value in my SQLite Database.
So I am using LocalDate.now().toString() and this is what I store as part of the row i.e. as TEXT column.
Now when I retrieve the row I want to be able to check if the last update was more than X days ago (not sure if X will be 1 or 7 yet).
How can I do this?
Most of the answers I see in google involve locale which I don't see it is relevenant to my case.
So I am stuck here:

// obj comes from db
LocalDate lastCheck = new LocalDate(obj.getLastUpdate());
//How can I do the check correctly?
if(Days.daysBetween(LocalDate.now(), lastCheck) > X days) {

}

Update:
In case it helps I don't need to be exact. Basically it is like a updating a cache so if there is 1 corner case that I check explicitely I am fine with that

Jim
  • 18,826
  • 34
  • 135
  • 254
  • Probably that thread will help you: http://stackoverflow.com/questions/17499987/java-how-to-calculate-accurate-time-difference-while-using-joda-time-jar – jakubbialkowski Jul 26 '15 at 17:42
  • If you want to use it for filtering, i.e. only give me the record if it's newer than X days, then you can let the DB do the work. `select * from table where last_update < now() - interval X day` – Maze Jul 27 '15 at 07:19
  • And why do you not use `Days.daysBetween(LocalDate.now(), lastCheck).getDays()` to make your comparison working resp. compilable? – Meno Hochschild Jul 28 '15 at 07:01

2 Answers2

1

if you are certain that the tow date are in the same year you can use

   LocalDate.now().getDayOfYear ()-lastCheck.getDayOfYear ()

Try this

    LocalDate last=LocalDate.of ( 2015, Month.DECEMBER, 31);
    LocalDate now= LocalDate.of ( 2016, Month.JANUARY, 1);
    java.time.Period period=java.time.Period.between ( last, now);
    System.out.println ( period.getDays () );
Billydan
  • 395
  • 1
  • 7
  • 25
  • 1
    If I check every X days (even for X = 1) I could end up in between a year. I mean 31/12- 1/1. This will be different years – Jim Jul 26 '15 at 14:38
0

The simpliest way to get the current date (and time) is just to create a new Date like this :

Date now = new Date();

Next, you want to store it to SQLite using the default mysql syntax. You can to it like this :

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = dateFormat.format(now);

Then, when you want to get back your stored date later from the mysql date syntax, you can do :

Date dateStored = dateFormat.parse(dateString)

As I can see you are using Joda Time library, you then can do :

DateTime dateTimeNow = DateTime.now();
DateTime dateTimeStored = new DateTime(dateStored)
Days days = Days.daysBetween(dateTimeStored, dateTimeNow);
int daysNumber = days.getDays()

And you have it !

victorleduc
  • 232
  • 2
  • 9
  • Why would I use the `SimpleDateFormat`? I don't need the timestamp – Jim Jul 26 '15 at 15:21
  • SimpleDateFormat allow to format dates into string values. The "yyyy-MM-dd HH:mm:ss" pattern given at the contructor of the SimpleDateFormat tell him to format it in a certain way, in this case the Mysql Way. No timestamps are involved in my answer. – victorleduc Jul 26 '15 at 15:30
  • But won't that include the exact time?That means that check in the same day but different times will fail right? – Jim Jul 26 '15 at 18:02
  • Yes it includes the exact time. If you test later in the same day, the int daysNumber will logically be 0. – victorleduc Jul 26 '15 at 20:35