1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
PushALU
  • 257
  • 1
  • 3
  • 15

1 Answers1

1

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243