?
Solved

running clock

Posted on 2003-03-10
5
Medium Priority
?
219 Views
Last Modified: 2010-03-31
Hi, I am new to Java and i'd like some help with this little program.
I know how to get the time from the system but i dont know how to keep the time updated, i.e. the seconds ticking etc. Could some one pls help,

Much Appreciated
0
Comment
Question by:ES5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
antonsigur earned 80 total points
ID: 8104243
okey, you know how to get the time from the system, good!

Now you need to do it every 1 sec or what?

then you just make the program "sleep" a while:

try {
  Thread.sleep(1000);
} catch (InterruptedException e) {
   e.printStackTrace();  
}

You can put this into a while(true) block.
like this:

while (true) {
  //Your code to get the time and display it
  try {
    Thread.sleep(1000);
  } catch (InterruptedException e) {
     e.printStackTrace();  
  }
}

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8110319
At any time, you can just use:

new Date ().getTime ()

to get the time in milliseconds from Jan 1, 1970, right then. So why do you need to keep your object ticking? Whenever you need the time, just make a new object and use the method.

Mayank.
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8110351
Yes he can, but he could be creating an applet wich displays time, then he needs to update the Applet's clock every second, won't he, so he could use this code to do it:

while (true) {
 //Your code to get the time and display it
 try {
   Thread.sleep(1000);
 } catch (InterruptedException e) {
    e.printStackTrace();  
 }
}
0
 

Author Comment

by:ES5
ID: 8113585
thanx antonsigur, the sleep function was all i needed just didnt know how to use it...

much appreciated
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113678
- good it worked for you!
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question