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 7942319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:56:11+00:00 2026-06-03T23:56:11+00:00

I have to calculate time difference in hours, minutes and seconds and update time

  • 0

I have to calculate time difference in hours, minutes and seconds and update time on my startup activity after each second. I am using this code.

              private Handler handler=new Handler() ;
private Runnable updateTimeTask = new Runnable() {
    @Override
    public void run() {

            updateTime();
            //handler.postDelayed(updateTimeTask, 1000);

    }
    };          @Override
protected void onResume() {
    super.onResume();



    int delay = 1000; // delay for 1 sec.

    int period = 1000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         handler.post(updateTimeTask);

    }

    }, delay, period);           }         
      private void updateTime() {

    final TextView time = (TextView) findViewById(R.id.txtViewTime);
    final TextView date = (TextView) findViewById(R.id.txtViewAlarmDesc);


    pref = new Prefs(this);
    String goal_dateTime= pref.getValue("first_goal_dateTime", "");

    Date d1 = Utils.strToDate(goal_dateTime);


    Calendar cal = Calendar.getInstance();
    if(d1!=null && d1.after(new Date()))
    {
    cal.setTime(d1);
    }
    Date cur = new Date();
    Calendar cal_cur = Calendar.getInstance();
    cal_cur.setTime(cur);


    final SimpleDateFormat formatterTime = new SimpleDateFormat("H:mm");

      long milliseconds1 = cal.getTimeInMillis();
      long milliseconds2 = cal_cur.getTimeInMillis();
      long diff = milliseconds1 - milliseconds2;
      long diffSeconds = diff / 1000;
      long diffMinutes = diff / (60 * 1000);
      long diffHours = diff / (60 * 60 * 1000);
      long diffDays = diff / (24 * 60 * 60 * 1000);


      time.setText(diffHours + " : " + diffMinutes+" : "+diffSeconds);
      date.setText(String.valueOf(diffDays+" days left to complete goal"));


}

But when i first time start activity , it works fine and when i start another activity from it and come back to it again , application just hangs and a black screen appears, and after much time it gives me errors like ANR, keydispatchtimeout.

I have tried several solutions like calling my updateTime() in a seperate thread but it gives me an error that “Only the original thread that created a view hierarchy can touch its views.”

any kind of help will be appreciated . Many thanks in advance.

Regards,
Munazza K

  • 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-03T23:56:12+00:00Added an answer on June 3, 2026 at 11:56 pm

    I suspect your problem may be that you’re not removing your callbacks when you leave the activity. I implemented the same thing in a slightly different way using postDelayed rather than scheduleAtFixedRate as below:

        private Handler h = new Handler();
        private static final long TIME_EVERY_SECOND = 1000;
    
        private Runnable onEverySecond = new Runnable() {
                public void run() {
                    updateTime();
                    h.postDelayed(onEverySecond, TIME_EVERY_SECOND);
                }
            };
    
            @Override
            public void onPause() {
                super.onPause();
    
                if (h != null) {
                    h.removeCallbacks(onEverySecond);
                }
            }
    
            @Override
            public void onResume() {
                super.onResume();
                h.postDelayed(onEverySecond, TIME_EVERY_SECOND);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ISO 8601 time string and want to calculate the time difference
I want to calculate the difference between two date/time in java using Date and
I have coded a simple PHP Countdown Timer using this code: list($date,$time)=explode( ,$compounddate); list($thour,$tminute,$tsecond)=explode(:,$time);
I have to calculate the average of each column in a specific nested list
I have code to calculate the percentage difference between 2 numbers - (oldNum -
I have this query to calculate the most common days of the week and
I have to support IE6 and I calculate rendering time by creating a timestamp
I have two times in an array and I need to calculate the difference
I am using opencart and want to display text (product ending time) like this:
I have a trouble with jquery each. this each block; $(document).ready( function(){ $('td.trh').each(function(index) {

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.