nef
nef

Reputation: 21

Time based calculation in Java

I am creating a program that makes a calculation every minute. I don't know exactly how to do this efficiently, but this is some pseudo code I have written so far:

 stockCalcTimerH = System.currentTimeMillis() - 1;
    stockCalcTimerI = stockCalcTimerH;
    stockCalcTimer = System.currentTimeMillis();

    if (stockCalcTimerI < stockCalcTimer) {
      *do calcuations*
     stockCalcTimerI + 60000;

When I print both values out on the screen, it comes out as this:

stockCalcTimerI = 1395951070595 stockCalcTimer = 1395951010596

It only subtracts the number, and doesn't add the 60000 milliseconds... I'm kind of new to Java, but any feedback helps.

Thanks for reading!!

Upvotes: 0

Views: 61

Answers (2)

ericbn
ericbn

Reputation: 10948

You can use java.util.Timer class to schedule runs.

TimerTask task = new TimerTask() {
    @Override public void run() {
        // do calculations
    }
};
int delay = 0;          // no delay, execute immediately
int interval = 60*1000; // every minute
new Timer().scheduleAtFixedRate(task, delay, interval);

Upvotes: 0

Obicere
Obicere

Reputation: 3019

stockCalcTimerI + 60000;

The new value never gets assigned to a variable.

Change that to:

stockCalcTimerI += 60000;

Which is the same as

stockCalcTimerI = stockCalcTimerI + 60000;

Upvotes: 2

Related Questions