Java Glossary : time

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : T words : time.

time
java.lang.System.currentTimeMillis() will give you the time in milliseconds since 1970 Jan 1 GMT. This is your best bet for computing elapsed times. Freshly minted java.util.Date objects contain the current GMT date/time stamp.

You may be interested in performance monitoring. You want to know how much CPU time your program used in various sections as opposed to elapsed time. For this you properly need help from the operating system or a profiler. The CPU is always being used by other tasks, the operating system, or even the system idle loop when you are not using it. You need OS help to avoid counting that time too. In a pinch, you have to add code that marks any time you start/return a call to the OS, so that you can discount that time. That still is not accurate because other tasks will interrupt you at any time, and because that scheme would not count the cpu time spent by the OS on your behalf.

The accuracy of System.currentTimeMillis() varies with platform. It is only nominally accurate to the millisecond.

55 ms Windows 9X
10 ms Windows NT, Windows 2000, Windows XP single processor
15.625 ms Windows NT, Windows 2000, Windows XP dual processor

In addition the Thread.sleep method will achieve delays of 1 ms on a single processor and 2 ms minimum on a dual processor.

In the Pentium there are RDTSC instructions to read special timestamp performance monitoring register which are more accurate than time of day clocks. RDTSC reads the cycle counter into EDX EAX. Some operating systems may make this a privileged instruction. See Intel's documentation for more details. You could access them via a bit of JNI. If you do that, make sure you discount the overhead of the JNI calls themselves.

If you want a task to be run at some time in the future or repeatedly at regular intervals, you can schedule it with the java.util.Timer class. See also javax.swing.Timer.

You can also use the JVMPI (Java Virtual Machine Profiler Interface) for accurate timing.

Units Of Time

Humans use a bizarrely complicated units of measure for time.

Imagine trying to explain this to a space faring species that used but a single unit of measure for time.

Unit Of Measure Definition
millenium 1000 years, 10 centuries.
century 100 years.
leap year the approximate time for the earth to revolve around the sun. 366 days.
year the approximate time for the earth to revolve around the sun. Sometimes 365 and sometimes 366 days.
season approximately 3 months, 1/4 year. Interval between solstice and equinox.
quarter approximately 3 months, 1/4 year. Business term for an arbitrary accounting period.
month 28 to 31 days.
fortnight 2 weeks, 7 days.
week 7 days.
day the approximate time for the earth to rotate on its axis. 24 hours.
hour 60 minutes, 1/24 of a day.
minute 60 seconds, 1/60 hour, 1/1440 day.
second 1000 milliseconds, 1/60 minute, 1/3600 hour, 1/86,400 day.
millisecond 1000 microseconds, 10-3 seconds, 1/1000 second, 1/3,600,000 hour, 1/86,400,000 day.
microsecond 1000 nanoseconds, 10-6 seconds, 1/1,000,000 second, 1/3,600,000,000 hour, 1/86,400,000,000 day.
nanosecond 1000 picoseconds, 10-9 seconds, 1/1,000,000,000 second, 1/3,600,000,000,000 hour, 1/86,400,000,000,000 day.
picosecond 1000 femtoseconds, 10-12 seconds, 1/1,000,000,000,000 second, 1/3,600,000,000,000,000 hour, 1/86,400,000,000,000 day.

In JDK 1.5+ you have System.nanoTime which has much greater resolution, but is not not synchronized to UTC time in any way. Presumably it is implemented with the Pentium RDTSC instruction in Windows.

Date ¤ gotchas for extensive hints on date/time processing ¤ Pentium RDTSC high resolution timer ¤ profiler ¤ SI prefixes such as pico and femto


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 3323.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/time.html J:\mindprod\jgloss\time.html