System.currentTimeMillis() / System.nanoTime() / System.exit()
Methods for retrieving the system time, measuring elapsed time, and controlling processes. For measuring execution time, System.nanoTime() is recommended.
Syntax
// Returns the current time as milliseconds since the UNIX epoch (1970-01-01 00:00:00 UTC).
long millis = System.currentTimeMillis();
// Used to measure high-resolution elapsed time (not an absolute timestamp).
long nano = System.nanoTime();
// Terminates the JVM (0 = normal exit, non-zero = abnormal exit).
System.exit(int status);
// Requests garbage collection (execution is not guaranteed).
System.gc();
// Retrieves an environment variable.
String path = System.getenv("PATH");
// Retrieves a system property.
String osName = System.getProperty("os.name");
String javaVer = System.getProperty("java.version");
Common Methods
| Method | Description |
|---|---|
| System.currentTimeMillis() | Returns the current time as milliseconds since the UNIX epoch (type long). |
| System.nanoTime() | Returns a high-resolution elapsed time in nanoseconds. The origin is arbitrary and not an absolute timestamp. Use this for measuring execution time. |
| System.exit(int status) | Terminates the JVM. 0 indicates normal exit; any other value indicates abnormal exit. |
| System.gc() | Requests garbage collection, but the timing of execution is up to the JVM. |
| System.getenv(String name) | Returns the value of the specified environment variable. Returns null if it does not exist. |
| System.getProperty(String key) | Retrieves a system property such as the OS name or Java version. |
| System.arraycopy(src, srcPos, dest, destPos, length) | Efficiently copies a portion of one array into another array. |
| System.lineSeparator() | Returns the line separator for the current OS (\n or \r\n). |
Sample Code
// Use currentTimeMillis() to get the current time in milliseconds.
long now = System.currentTimeMillis();
System.out.println("Current time (ms): " + now);
// Use nanoTime() to measure execution time.
long start = System.nanoTime();
long sum = 0;
for (int i = 0; i < 1_000_000; i++) sum += i;
long end = System.nanoTime();
System.out.println("Sum: " + sum);
System.out.printf("Elapsed time: %.3f ms%n", (end - start) / 1_000_000.0);
// Example output: "Elapsed time: 2.456 ms"
// Convert currentTimeMillis() to a Date object.
import java.util.Date;
Date date = new Date(System.currentTimeMillis());
System.out.println(date); // Prints the current date and time.
// Use getProperty() to retrieve system information.
System.out.println(System.getProperty("os.name")); // Example output: "Mac OS X"
System.out.println(System.getProperty("java.version")); // Example output: "21.0.2"
System.out.println(System.getProperty("user.home")); // Prints the path to the home directory.
// Use arraycopy() to efficiently copy an array.
int[] src = {1, 2, 3, 4, 5};
int[] dest = new int[5];
System.arraycopy(src, 1, dest, 0, 3); // Copies src[1] through src[3] (2, 3, 4) into dest starting at index 0.
System.out.println(java.util.Arrays.toString(dest)); // Output: "[2, 3, 4, 0, 0]"
Notes
For measuring execution time, System.nanoTime() is recommended over System.currentTimeMillis() due to its higher precision. The return value of nanoTime() represents nanoseconds elapsed from an arbitrary system origin, so only the difference between two calls is meaningful.
System.exit() immediately terminates the entire JVM, which may prevent try-finally blocks and shutdown hooks from running. Avoid using it in normal applications; use exception handling for flow control instead.
For writing to standard output, see System.out.println() / System.err.println().
If you find any errors or copyright issues, please contact us.