How do I calculate process elapsed time?

This example shows us how to use the System.nanoTime() method to get the amount of time a process take place. Please be aware that the nano time value is not related to the real world time value.

package org.kodejava.example.lang;

public class ElapsedTimeExample {
    public static void main(String[] args) {
        // Get the start time of the process
        long start = System.nanoTime();
        System.out.println("Start: " + start);

        // Just do some a bit long process calculating the total value
        // of even number from zero to 10000
        int totalEven = 0;
        for (int i = 0; i < 10000; i++) {
            if (i % 2 == 0) {
                totalEven = totalEven + i;

        // Get the end time of the process
        long end = System.nanoTime();
        System.out.println("End  : " + end);

        long elapsedTime = end - start;

        // Show how long it took to finish the process
        System.out.println("The process took approximately: "
            + elapsedTime + " nano seconds");

And example of the result are:

Start: 14113495997955
End  : 14113497072393
The process took approximately: 1074438 nano seconds

How can I get current working directory?

A system properties named user.dir can be used if you want to find the current working directory of your Java program.


public class CurrentDirectoryExample {
    public static void main(String[] args) {
        // System property key to get current working directory.
        String USER_DIR_KEY = "user.dir";
        String currentDir = System.getProperty(USER_DIR_KEY);

        System.out.println("Working Directory: " + currentDir);

Result example:

Working Directory: /Users/wsaryada/Projects/kodejava

How do I get Java Runtime Environment (JRE) version?

This code snippet show you how to get JRE version by reading the java.version from the system properties. We use the System.getProperty(String key) method to get the value of the property and it will be returned as a String value.

package org.kodejava.example.lang;

public class JavaVersion {
    public static void main(String[] args) {
        // You can get the version of Java Runtime Environment
        // running on your machine by reading the "java.version"
        // property value from the system.
        String version = System.getProperty("java.version");

        // Just show to the world your JRE version :)
        System.out.println("JRE Version = " + version);

In my computer it print the following output:

JRE Version = 1.8.0_20