long start = System.currentTimeMillis(); // code to be timed long end = System.currentTimeMillis(); long elapsedTime = end - start; System.out.println("Elapsed Time: " + elapsedTime + " milliseconds");
import java.util.Date; long timeStamp = System.currentTimeMillis(); Date date = new Date(timeStamp); System.out.println("Current Date and Time: " + date.toString());This example uses the current time in milliseconds to create a Date object representing the current date and time. The System.currentTimeMillis() method is part of the java.lang package.