I have to write a program that mimics a digital clock w/ seconds, minutes, and hours. I'm sure I can get the displays working on my own and everything. My concern: what is the most efficient way to check the time? Should I just do Thread.sleep for 1 second then update things accordingly? Or should I use Java's date and time classes to do this? If I use the date & time in, say, a while loop, it would be inefficient since I really only need to check to see if something is a new time every second.

There are a couple of different ways you can do it. Either of the provided Timer classes can be used to schedule a repeated task to update your internal variables (i.e. seconds). You can read about when to use which here. Alternately, you could have a simple thread running a loop that sleeps 1000 ms, updates your time variables, and schedules a repaint on the the AWT event queue (with invokeLater())

Any of these methods can slip a little time over the long run if you update your internal time with something as simple as seconds++; due to process time slicing, garbage collection, etc. If you want your time to remain more accurate over longer periods, you can update your instance time variables from the difference in System.nanotime() since your last update.

Thanks for the advice. I'm not concerned with "long term" correctness of the time - so I'll probably go with a Timer. And I don't need to schedule a repaint, since I'm using a String to write the time.

Oh, forgot to mark solved. Also - Ezzaral - My problem doesn't require as exact a solution as you suggested, but I am curious what you meant by the nanoseconds thing. Is this what you were saying?

while(true){
sleep for 1 second
take last measured nanoseconds - current nanoseconds
do adjustment to the current second, if necessary
}

Yes exactly, you'd calc the actual elapsed time instead of assuming that just because you slept 1 second (or whatever increment) that only 1 second had elapsed.

This question has already been answered. Start a new discussion instead.