Previous | Next | Trail Map | The Java Development Environment | Using System Resources


Miscellaneous System Methods

Getting the Current Time

The currentTimeMillis() method returns the current time in milliseconds since 00:00:00 UTC, January 1, 1970. The currentTimeMillis() method is commonly used during performance tests: get the current time, perform the operation that you want to time, get the current time again--the difference in the two time samples is "roughly" the amount of time that the operation took to perform.

Often in graphical user interfaces the time difference between mouse clicks is used to determine whether a user double clicked in the window. The following applet uses currentTimeMillis() to compute the number of milliseconds between two mouse clicks. If the time period between the clicks is smaller than 200 milliseconds, the two mouse clicks are interpreted as a double mouse click.

Here's the source for the TimingIsEverything applet shown above.

You could use the return value from this method to compute the current date and time. However, you'll probably find that it's more convenient to get the current date and time from the Date class.

You may have noticed that System supports two other time-related methods besides the currentTimeMillis() method: currentTime() and nowMillis(). Both are obsolete--you should use the currentTimeMillis() method instead. currentTime() and nowMillis() may not be supported in future versions of the System class.

Exiting the Runtime Environment

To exit the Java interpreter, call the System.exit() method. You pass an integer exit code to the exit() method, and the interpreter exits with that exit code.
Note: The exit() method causes the Java interpreter to exit, not just your Java program. You should use this function with caution, particularly in applets.


Previous | Next | Trail Map | The Java Development Environment | Using System Resources