4

On my Android App, I'd like to only re-import my data if it's been at least X hours since the last import.

I'm storing the last_updated time in my sqlite database in this format: 2012/07/18 00:01:40

How can I get "hours from then" or something like that?

My code thus far:

package com.sltrib.utilities;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateHelper
{

  public static String now()
  {
      Calendar currentDate = Calendar.getInstance();
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      String dateNow = formatter.format(currentDate.getTime());
      //System.out.println("Now the date is :=>  " + dateNow);
      return dateNow;
  }

  public static int hoursAgo(String datetime)
  {
      //return the number of hours it's been since the given time
      //int hours = ??
      //return hours;
  }

}
Dave
  • 28,833
  • 23
  • 113
  • 183
  • You could just store the time in ms. Then you just have to compute the difference and devide it by the number of mmilliseconds per hour – jobnz Jul 18 '12 at 00:32

2 Answers2

9

You're going to want to do math between two Calendars or Dates.

Note: Aspects of Date are deprecated, see below for Calendar!

Here's an example using Date:

public static int hoursAgo(String datetime) {
    Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH).parse(datetime); // Parse into Date object
    Date now = Calendar.getInstance().getTime(); // Get time now
    long differenceInMillis = now.getTime() - date.getTime();
    long differenceInHours = (differenceInMillis) / 1000L / 60L / 60L; // Divide by millis/sec, secs/min, mins/hr
    return (int)differenceInHours;
}

There are some try/catch blocks involved here (which you should probably handle with throws), but this is the basic idea.

Edit: Since parts of Date are deprecated, here is the same method using Calendar:

public static int hoursAgo(String datetime) {
    Calendar date = Calendar.getInstance();
    date.setTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH).parse(datetime)); // Parse into Date object
    Calendar now = Calendar.getInstance(); // Get time now
    long differenceInMillis = now.getTimeInMillis() - date.getTimeInMillis();
    long differenceInHours = (differenceInMillis) / 1000L / 60L / 60L; // Divide by millis/sec, secs/min, mins/hr
    return (int)differenceInHours;
}
Cat
  • 66,919
  • 24
  • 133
  • 141
  • Thanks - I think this is working - going to double check in the morning and will reply/mark/update...etc – Dave Jul 18 '12 at 02:14
  • I changed it to minutes (and compared against 60) since I wasn't sure if 90 minutes would return 1 or 2 hours...etc. So.. just removed the last 60L and good to go - it works! THanks! – Dave Jul 18 '12 at 02:35
  • Good to hear! Just an FYI, 90 minutes would be 1 hour (due to integer rounding). But if minutes works for you, that's a perfect solution. :) – Cat Jul 18 '12 at 02:37
  • When I pass it a string like, 05:23 pm, I get big number like 400270 ??? And I changed it to SimpleDateFormat("hh:mm aa", .... to mach the format I'm passing. – fullmoon Aug 31 '15 at 22:37
  • @fullMoon More than likely it thinks that, since you didn't specify a date, you are asking for Jan 01, 1970, 5:23pm... so 400,000 hours ago seems pretty accurate. – Cat Aug 31 '15 at 22:49
0

You can also do it directly on the query. Look this question, there is examples of how to calculate difference between two dates:

SQLite: express the difference as days, hours, minutes between two given dates

Community
  • 1
  • 1
Caio Cunha
  • 23,326
  • 6
  • 78
  • 74