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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:18:19+00:00 2026-05-16T10:18:19+00:00

I have the required code for the stopwatch here. All i want is get

  • 0

I have the required code for the stopwatch here. All i want is get rid of the Swing part here and display the same output in console. Can anybody help?

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.text.*;

public class ElapsedTime extends JFrame
{
JLabel time;

long startTime = System.currentTimeMillis();

ElapsedTime()
{
setSize(380,200);
setTitle("http://simpleandeasycodes.blogspot.com/");
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new GridBagLayout());

time = new JLabel("");

time.setFont(new Font("SansSerif",Font.BOLD, 36));

time.setForeground(Color.MAGENTA);

add(time);

//starting new Thread which will update time
new Thread(new Runnable()
{
public void run() 
{ try 
{
updateTime(); 
} 
catch (Exception ie) 
{ }
}
}).start();
}

public void updateTime()
{
try
{
while(true)
{
//geting Time in desire format
time.setText(getTimeElapsed());
//Thread sleeping for 1 sec
Thread.currentThread().sleep(1000);
}
}
catch (Exception e)
{
System.out.println("Exception in Thread Sleep : "+e);
}
}

public String getTimeElapsed()
{
long elapsedTime = System.currentTimeMillis() - startTime;
elapsedTime = elapsedTime / 1000;

String seconds = Integer.toString((int)(elapsedTime % 60));
String minutes = Integer.toString((int)((elapsedTime % 3600) / 60));
String hours = Integer.toString((int)(elapsedTime / 3600));

if (seconds.length() < 2)
seconds = "0" + seconds;

if (minutes.length() < 2)
minutes = "0" + minutes;

if (hours.length() < 2)
hours = "0" + hours;

return minutes+":"+seconds;
}

public static void main(String[] args) 
{
JFrame obj = new ElapsedTime();
obj.setVisible(true);
}
}
  • 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-16T10:18:19+00:00Added an answer on May 16, 2026 at 10:18 am

    The keys are:

    a.) Finding which character to write to the console in order to remove the most recently-written character (\b, or \010 in ASCII)
    b.) Realising that you need to remember how many characters you’ve written to the console the last time you updated it
    c.) Remembering to use print instead of println

    public class Test {
    
      public static void main(String[] args) throws InterruptedException {
        int charsWritten = 0;
        long start = System.currentTimeMillis();
        while (1 > 0) {
          Thread.sleep(1000);
          long elapsedTime = System.currentTimeMillis() - start;
          elapsedTime = elapsedTime / 1000;
    
          String seconds = Integer.toString((int) (elapsedTime % 60));
          String minutes = Integer.toString((int) ((elapsedTime % 3600) / 60));
          String hours = Integer.toString((int) (elapsedTime / 3600));
    
          if (seconds.length() < 2) {
            seconds = "0" + seconds;
          }
    
          if (minutes.length() < 2) {
            minutes = "0" + minutes;
          }
    
          if (hours.length() < 2) {
            hours = "0" + hours;
          }
    
          String writeThis = hours + ":" + minutes + ":" + seconds;
    
          for (int i = 0; i < charsWritten; i++) {
            System.out.print("\b");
          }
          System.out.print(writeThis);
          charsWritten = writeThis.length();
        }
      }
    }
    

    Note: you could be more efficient by only clearing the console up to only the characters you are changing but I figure you’re not going to get that much of a speed improvement.

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

Sidebar

Related Questions

No related questions found

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.