Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

running clock

Posted on 2003-03-10
5
Medium Priority
?
224 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
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 Month14 days, 6 hours left to enroll

580 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