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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:13:53+00:00 2026-05-23T23:13:53+00:00

I want to perform a task using Java at specific, given hour . My

  • 0

I want to perform a task using Java at specific, given hour. My code works, but it doesn’t look good. I’m comparing two strings. I tried to compare dates using getTimeInMills() but numbers aren’t the same.

Here’s my code:

import java.util.Calendar;

class Voter {
Calendar current,deadline;
boolean keepGoing = true;

public static void main(String[] args) {
    Voter strzal = new Voter();
    strzal.shoot();
}

public void shoot() {
    deadline = Calendar.getInstance();
    deadline.set(2011,6,21,11,20,00);

    while (keepGoing) {
        // wait 1 second
        try {
            Thread.sleep(1000);
            System.out.print(".");
        } catch (InterruptedException ex) { ex.printStackTrace(); }

        current = Calendar.getInstance();

        if (deadline.getTime().toString().equals(current.getTime().toString())) {
            System.out.println("Got it!");
            keepGoing = false;
        } // end of if
    } // end of while
} // end of shoot() method

}

I’m running in the loop, waiting for the current time to be equal to deadline (in an example I set it to 21st July 2011 11:10 AM).

How can I improve my code? I’d like to use for example compareTo() or getTimeInMills() method from Calendar class, but I can’t set deadline properly.

Thanks in advance.

  • 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-23T23:13:54+00:00Added an answer on May 23, 2026 at 11:13 pm

    Using a ScheduledExecutorService is a better suggestion, however for your knowledge

    Calendar deadline = Calendar.getInstance();
    deadline.set(2011,6,21,11,20,00);
    long endTime = deadline.getTime().getTime();
    
    // later
    long current = System.currentTimeMillis();
    if (current >= endTime) {
        System.out.println("Got it!");
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JAVA framework i want to achieve the following task. hot code(jar) deployment which
I want to perform the following task using JSF / Richfaces On click of
I want to perform a simple task ! but dont know if it is
I want to perform cascade delete for some tables in my database, but I'm
I want to create a Java application bundle for Mac without using Mac. According
I have a task to perform an HttpWebRequest using Task<WebResponse>.Factory.FromAsync(req.BeginGetRespone, req.EndGetResponse) which can obviously
i want to perform some test by using Json . I am using google
I want to perform some empirical trade-off's to assess the performance of applications written
I have a service written in C# (.NET 1.1) and want it to perform
I want to override OnMouseClick and OnMouseDoubleClick and perform different actions depending on which

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.