time : Java Glossary
home T words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-01-05 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 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 (all)
time
java.lang..System.currentTimeMillis () will give you the time in milliseconds since 1970 Jan 1 0:00 GMT. This is your best bet for computing elapsed times. Freshly minted java.util.Date objects contain the current GMT date/time stamp as do freshly minted java.util. GregorianCalendar objects.
Benchmarking Timer Accuracy Learning More
Pentium RDTSC Units Of Time Links
Non-Java Schemes Mixed Base Conversion
Performance Monitoring Repeating Events

Benchmarking, Measuring Elapsed Time

For benchmarking, in JDK 1.5+ you have System. nanoTime:
// elapsed time with System.nanoTime
// Works only in JDK 1.5+

long start = System.nanoTime();

// .. do something

// elapsed time in nanoseconds, billionths of a second
long elapsed = System.nanoTime() - start;
which has much greater resolution than System.currentTimeMillis, but is not synchronized to UTC time in any way. There is no guarantee of resolution. You may well find every value it returns ends in 000 because the clock cannot provide better than millisecond resolution. On a 2 GHz machine, 1 nanosecond represents about two machine cycles or the time at takes to do two additions.

On Windows boxes, a 1.5 JVM uses the Win32 function QueryPerformanceCounter() which has has no defined resolution. It reports clock cycles since bootup. The JVM has to use QueryPerformanceFrequency() to interpret the results. The JVM does this for you and reports the results of System. nanoTime in true nanoseconds. If you want the finer resolution of clock cylcles, use RDTSC. This means that System. nanoTime results are useful both for comparing algorithms on the same hardware and for benchmarking different hardware on the same algorithm.

Unfortunately, if you have multiple CPUs each has its own nanotimer, and Windows does not properly keep them synchronised. So you hop back and forth between different timers as your task runs on different CPUs. Linux has ways of dealing with this. Power Management also causes drift between muliple CPUs.

If you have to code for 1.4-, then you can only get millisecond accuracy. You need to use:

// elapsed time with System.currentTimeMillis
// Works in all JDK versions

long start = System.currentTimeMillis();

// .. do something

// elapsed time in microseconds, millionths of a second
long elapsed = System.currentTimeMillis() - start;
Watch the spelling of System.currentTimeMillis(). If you are like me you will be tempted to spell it as System. getCurrentTimeMillis() or System. currentTimeInMillis () or System. timeInMillis().

Pentium RDTSC

The Pentium has a hardware instruction RDTSC Read Time Stamp Counter. It returns the number of clock cycles since the CPU was powered up or reset. This is subnanonsecond resolution! a 4GHz clock would give you 4 tick counts per nanosecond. This hardware, and similar features on other CPUs, is what makes System.nanoTime possible. One nice feature of the counter is that adjusting the time of day clock does not disturb it is any way.

If you are stuck using a JDK 1.4-, then you could roll your own nanoTime method. 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 with the Pentium class that I wrote and posted the code for. If you use that method, make sure you calibrate so you can discount the considerable overhead of the JNI calls themselves.

Non-Java Timestamp Schemes

There are other timestamp schemes you will run into. E.g. OpenType measures time with 64-bit seconds since 0:00 midnight, 1904-01-01 UTC.

Microsoft timestamps files with 64 bit hundreds of nanoseconds since 1601-01-01 UTC Gregorian. I presume they use Pope Gregory’s flip date to avoid the complication of the missing days. The FileTimes package converts from Java time to Windows native time to access the create date and last access date of a Windows file. In DOS/Win 3.1/W95/W98/Me the dates were kept internally in local time.

Performance Monitoring

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.

Timer Accuracy

The accuracy of System.currentTimeMillis() varies with platform. It is only nominally accurate to the millisecond.
Timer Resolution
Resolution Platform
55 ms Windows 95/98
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.

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
millennium 1000 years, 10 centuries.
century 100 years.
leap year the approximate time with respect to the sun, for the earth to revolve around the sun 366 days.
year the approximate time with respect to the sun, 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, 14 days.
week 7 days.
day the approximate time for the earth to rotate on its axis. 24 hours. In civil time, the day may be 23 or 25 hours long during daylight saving switch over in jurisdictions that use a 60 minute daylight saving offset. Using 86,400,000 ms for a civil day will get you in trouble spanning daylight saving switch over if you use local time rather than pure UTC.
hour 60 minutes, 1/24 of a day.
minute 60 seconds, 1/60 hour, 1/1440 day. It can occasionally be 59 or 61 seconds long when leap seconds are inserted. Java ignores leap seconds, treating them as physical imperfections of the clocks that are adjusted, but not tracked.
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.

Mixed Base Conversion

If you study the code and understand how it works, you should be able to write your own mixed base conversions for hours:minutes:seconds, miles:yards:feet:inches etc.

Repeating Events

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. or also javax.swing.Timer. See Timer for details.

Learning More

Sun’s Javadoc on the currentTimeMillis method : available:
Sun’s Javadoc on the nanoTime method : available:
Sun’s Javadoc on the sleep method : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] Spread the Net
You are visitor number 43,870.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/time.html J:\mindprod\jgloss\time.html