Q:

Measure the time it takes for Java class to run

Is it possible to start a timer in a Java class, so that I may measure the time it takes for its task?
I tried with
Date d2 = (Date) session.getAttribute("date2");
Date d1 = new Date();

long seconds = (d2.getTime()-d1.getTime())/1000;
System.out.println("time: " + seconds);

Date dates = new Date();                
session.setAttribute("date2", dates);

But it's giving me some wierd negative numbers, that I cannot make sense off.

A:

it's giving me some wierd negative numbers

d2 is going to be before d1:

d2 was "now" the last time the method was called in this session
d1 is "now" this time the method was called.

As such, d2.getTime() < d1.getTime().
Reverse the order of the subtraction.

