Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:14:33+00:00 2026-06-07T21:14:33+00:00

On my Android App, I’d like to only re-import my data if it’s been

  • 0

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;
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:14:35+00:00Added an answer on June 7, 2026 at 9:14 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My android app is taking more and more memory over time. I took a
My Android app requires to get the current date and time from Internet .
import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts.People;
My Android app needs to populate the ListView using the data from an ArrayList
I wrote an android app, which requires large amount of data to be transfered
My Android app has a layout that looks like this: --------------------- | | |
My Android app sends/retrieves data to/from the user's own PC using HTTP and it's
import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button;
Android app gives me an error now: package com.martijngijselaar.rooster; import android.os.Bundle; import android.view.MotionEvent; import
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.RelativeLayout; public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.