Using System Resources |
Getting the Current Time
ThecurrentTimeMillis()
method returns the current time in milliseconds since 00:00:00 UTC, January 1, 1970. ThecurrentTimeMillis()
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()
andnowMillis()
. Both are obsolete--you should use thecurrentTimeMillis()
method instead.currentTime()
andnowMillis()
may not be supported in future versions of the System class.Exiting the Runtime Environment
To exit the Java interpreter, call theSystem.exit()
method. You pass an integer exit code to theexit()
method, and the interpreter exits with that exit code.
Note: Theexit()
method causes the Java interpreter to exit, not just your Java program. You should use this function with caution, particularly in applets.
Using System Resources |