Results 1 to 6 of 6
  1. #1
    siva_vs_siva is offline Member
    Join Date
    Aug 2010
    Posts
    31
    Rep Power
    0

    Default 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());

    }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    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

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    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() method
    Last edited by joshdgreen; 10-29-2010 at 08:25 AM.
    Sincerely, Joshua Green
    Please REP if I help :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    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

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Quote Originally Posted by Darryl.Burke View Post
    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

    Yes, I did, thanks. I made a little edit.
    Last edited by joshdgreen; 10-29-2010 at 08:24 AM.
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    siva_vs_siva is offline Member
    Join Date
    Aug 2010
    Posts
    31
    Rep Power
    0

    Default

    can you tell about deprecated methods?

    but GregorianCalendar also doesn't allow

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 01:46 PM
  2. threading execution time question
    By centenial in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-09-2010, 06:49 AM
  3. Replies: 2
    Last Post: 11-04-2008, 03:48 AM
  4. A question about displaying an image...
    By SpaceY in forum New To Java
    Replies: 0
    Last Post: 08-24-2008, 07:50 PM
  5. Urgent-Imp-Displaying message with respect to system time
    By garinapavan in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 03:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •