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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:43:03+00:00 2026-06-12T09:43:03+00:00

I am making a small alarm clock application to brush up on Java. The

  • 0

I am making a small alarm clock application to brush up on Java. The purpose of the app is to let the user set the clock time and alarm time, and then the alarm should “go off” when the system time is equal to the alarm time.

The clock class contains a Calendar and Date object.

The clock is referred to from a main method outside the class. In that main method, I have built a simple command line user interface, where the user can set the time of the clock and alarm. By default, the clock is initialized to the current system time.

However, here is where the problem is for both the automatically initialized and the user defined clock objects – they aren’t “ticking” or updating.

Clock regularClock = new Clock(); //Defaults clock (using a Date) to the system time

while (userInput != "Quit")
{
   switch(userInput)
   {
      ...Other choices here...
      case "Set Time":  System.out.print("Enter hour: ");
                    hours = kb.nextInt();
                System.out.print("\nEnter minutes: ");
                minutes = kb.nextInt();
                ACR.regularClock.setTime(hours, minutes);
                System.out.println("Clock has been set");

       case "Set Alarm": System.out.print("Enter hour: ");
            hours = kb.nextInt();
            System.out.print("\nEnter minutes: ");
            minutes = kb.nextInt();
            ACR.alarmClock.setTime(hours, minutes);
            ACR.alarmClock.setAlarmOn(true);
            System.out.println("Alarm has been set.");
            break;
      ...Other choices here...

  userInput = keyboard.next();
   }

As you will see, there are no loops or anything to refresh or keep the regularClock ticking. For some reason, when I started I thought Date and Calendar objects just kept running once created – sort of like a stopwatch.

So now I’m wondering what the best way to update them would be, in this while loop. If only the default system time clock was allowed, it would be easy – I could just create a new Date object at the beginning of the while loop each time. However, that would override the user created clock time if they chose that.

Also, if the user weren’t to enter any input – and instead just let the app sit there – where he/she would enter input – shouldn’t it still be refreshing the times and checking if the regularClock = alarmClock time? How can I do this?

I realize I’m sort of rambling now, so I’ll leave it at that. I’ve been working at this but can’t figure out the best solution. If you have any questions, please let me know!

Short summary questions:

  1. How do I keep a the time in a Date or Calendar object ticking, even when it has been modified?

  2. How can I continuously update these objects, while waiting for user input?

  • 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-12T09:43:05+00:00Added an answer on June 12, 2026 at 9:43 am

    There are easier ways, but that’s not the question 😉

    Basically, you need to establish some kind of “tick” thread that can update/modify the clock in the background…

    You can write your own Thread / Runnable to perform these tasks, but they are inherently inaccurate…

    Something like…

    Thread thread = new Thread(new Ticker());
    thread.setDaemon(true); // Otherwise the JVM won't stop when you want it t
    thread.start();
    
    //...
    
    public class Ticker implements Runnable {
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException exp) {
                }
                // Update static reference to the clock...
            }
        }
    }
    

    Beware though

    Causes the currently executing thread to sleep (temporarily cease
    execution) for the specified number of milliseconds, subject to the
    precision and accuracy of system timers and schedulers. The thread
    does not lose ownership of any monitors.

    Another way would be use the java.util.Timer

    Timer timer = new Timer(true);
    timer.scheduleAtFixedRate(new Ticker(), 1000, 1000);
    
    //...
    
    public class Ticker extends TimerTask {
        public void run() {
            // Update static reference to the clock...
        }
    }
    

    Again, beware…

    Subsequent executions take place at approximately regular intervals

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

Sidebar

Related Questions

I'm making a small tool application that user can minimize in the taskbar .
I'm making a small web app in which a user enters a server URL
I am making a small section of an app (phone and website) that can
I'm making a small on-line clock (both for fun and to learn a bit
Im making a small private message application in the form of a phone. Ten
Im making a small user control, i was wondering if it was possible to
I'm making a small simple windows application. This is my main function: static void
I'm making small app for myself, and I want to find strings which match
So I am making a small site using cakephp, and my ACL is set
I am making a small application where I am receiving images from web-service. I

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.