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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:06:58+00:00 2026-05-23T20:06:58+00:00

I would like to start a task at a specific time. For that I

  • 0

I would like to start a task at a specific time. For that I use runnable and postDelayed method as follows:

private Runnable mLaunchTask = new Runnable() {
        public void run() {

            try {
                MY TASK
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
};

In my code I use mLunchTask as follows:

mHandler = new Handler();
mHandler.postDelayed(mLaunchTask, myDelay*1000); 

and myDelay is computed as follows:

s  = DateFormat.format("hh:mm:ss aaa", d.getTime());
cTime = s.toString(); // current Time
ch = Integer.parseInt(cTime.substring(0,2)); // current hour
cm = Integer.parseInt(cTime.substring(3,5)); // current minute
cs = Integer.parseInt(cTime.substring(6,8)); // current second
if (cTime.substring(9,11).equalsIgnoreCase("pm") && (ch<12) ) ch = ch+12; 
myDelay=(desiredHour-ch)*3600+(desiredMinute-cm)*60 - cs;
if (myDelay<0) myDelay = 0;

and desiredHour and desiredMinute are set by user. The expectation is that MY TASK starts at desiredHour and desiredMinute and 0 seconds.
However “MY TASK starts with a few seconds delay, which looks like is random.

Based on the above code, is there any reason that it does not start at the exact desired time?

Thanks

  • 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-05-23T20:07:00+00:00Added an answer on May 23, 2026 at 8:07 pm

    First of all, your delay calculation is correct, but the “pm” detection doesn’t work with other languages. It is much better to use the calendar to get the delay:

    Calendar calendar = Calendar.getInstance();
    long currentTimestamp = calendar.getTimeInMillis();
    calendar.set(Calendar.HOUR_OF_DAY, desiredHour);
    calendar.set(Calendar.MINUTE, desiredMinute);
    calendar.set(Calendar.SECOND, 0);
    long diffTimestamp = calendar.getTimeInMillis() - currentTimestamp;
    long myDelay = (diffTimestamp < 0 ? 0 : diffTimestamp);
    

    Now you have the delay in milli secs and can start the handler:

    new Handler().postDelayed(mLaunchTask, myDelay);
    

    In my test the following runnable logs at the desired time with no delay.

    private Runnable mLaunchTask = new Runnable() {
        public void run() {
            Calendar calendar = Calendar.getInstance();
            Log.d("test", "started at " 
                + calendar.get(Calendar.HOUR_OF_DAY) + " " 
                + calendar.get(Calendar.MINUTE) + " "
                + calendar.get(Calendar.SECOND)
            );
        }
    };
    

    Maybe your started task needs some seconds to be loaded?

    Could the AlarmManager be an alternative?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to start a personal project that will give me a lot
I have a project that I would like to start beta testing soon, it
I've been developing my first iPhone app part-time and would like to start using
I would like to get the status of a Rake Task into my backoffice(That
Hi there I would like to start by saying that i'm a beginner, but
I would like to use the popular Thor gem to create a daemonized task.
I have a background task I would like to handle. The thing is that
I have a high CPU/memory bound task that I would like my Scala program
I would like start with .NET Micro Framework as my hobby project. What hardware
I would like to start tagging my deployed binaries with the latest SVN revision

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.