Results 1 to 6 of 6
Thread: Question for Displaying time
- 10-29-2010, 05:51 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 31
- Rep Power
- 0
Question for Displaying time
how to display the time continuously....
i am using timer.. but it sometime doesn't work and throws the Timer-0 exception. how to solve this problem and how can i achieve in another way.. my code is..
new Timer().schedule(new datedisplay(),0,1000);
//class
class datedisplay extends TimerTask
{
public void run()
{
java.util.Date time=new java.util.Date();
System.out.println(time.getHours()+":"+time.getMin utes()+":"+time.getSeconds());
}
}
- 10-29-2010, 06:09 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
There is no Timer-0 exception, Timer-0 is the thread name! Please give use the full stacktrace!
btw: The methods which you are invoke are deprecated, read the API doc of Date
- 10-29-2010, 06:16 AM #3
Here's the best I could muster in a few minutes:
EDIT: Method is deprecated, use only for reference or to get better ideas:
Java Code:class NewClass { public static void main(String args[]) throws InterruptedException { boolean done = false; // Variable used for infinite loop while(done == false) // done will always be false so keep looping. { Thread.currentThread().sleep(1000); // 1 second in between updated times. java.util.Date time = new java.util.Date(); System.out.println(time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds()); } } }
It's pretty much what you had before, I just added an infinite loop and a wait time of 1 second before each updated time. I put comments in the code that you can read. Hope this helps. I got information on sleep() from here: How to use sleep() methodLast edited by joshdgreen; 10-29-2010 at 07:25 AM.
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 07:19 AM #4
Josh, did you miss eRaaaa 's remark about deprecated methods?
Siva, don't use Josh's code. Take a look at java.util.Calendar / GregorianCalendar where you'll find a method that's not deprecated.
db
- 10-29-2010, 07:20 AM #5
- 10-29-2010, 08:22 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
threading execution time question
By centenial in forum Threads and SynchronizationReplies: 4Last Post: 09-09-2010, 05:49 AM -
Please tell me I am not crazy... Time Complexity (Big-O) Question
By Jordan in forum New To JavaReplies: 2Last Post: 11-04-2008, 02:48 AM -
A question about displaying an image...
By SpaceY in forum New To JavaReplies: 0Last Post: 08-24-2008, 06:50 PM -
Urgent-Imp-Displaying message with respect to system time
By garinapavan in forum New To JavaReplies: 1Last Post: 08-03-2007, 02:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks