Comparint timestamps

S

sconeek

Hi all,

I am trying to compare 2 timestamps. The idea is that if the last
modified date is longer than 10 seconds display an error message. Now I
am comparing 2 timestamps modifiedDate and lastEditTimeStamp and if
their difference is greater than 10000 (10sec) display a message, but
this doesnt seem to work. Can some genius please help me out.

The other approach is that, every 8 seconds check if the last modified
date has changed or not and then display a message accordingly.

This is what I have done so far,
if ((modifiedDate.getTime() - lastEditTimeStamp.getTime()) > 10000)
{
Debug.println("BUG 001");
this.setSysStatus(CONTROL_ERROR_MSG);
}

All help will be greatly appreciated.

Cheers.
 
R

Roedy Green

if ((modifiedDate.getTime() - lastEditTimeStamp.getTime()) > 10000)

It is so frustrating when people leave off the declarations from code
snippets. Half the time the problem is with the declarations. And you
can't make sense of procedural code without the declarations.
 
S

sconeek

my bad.
Date modifiedDate = new Date();
Date lastEditTimeStamp = (Date) curRbtMpInf
.get(rbt_map.LAST_EDIT_TIMESTAMP);

hope this helps.
 
R

Rhino

sconeek said:
Hi all,

I am trying to compare 2 timestamps. The idea is that if the last
modified date is longer than 10 seconds display an error message. Now I
am comparing 2 timestamps modifiedDate and lastEditTimeStamp and if
their difference is greater than 10000 (10sec) display a message, but
this doesnt seem to work. Can some genius please help me out.

The other approach is that, every 8 seconds check if the last modified
date has changed or not and then display a message accordingly.

This is what I have done so far,
if ((modifiedDate.getTime() - lastEditTimeStamp.getTime()) > 10000)
{
Debug.println("BUG 001");
this.setSysStatus(CONTROL_ERROR_MSG);
}

All help will be greatly appreciated.

Cheers.

First of all, I'm assuming you used java.sql.Timestamp, not
java.security.Timestamp. Please verify that you are also using
java.sql.Timestamp.

I used the following code and observed some interesting results:

Timestamp modifiedDate = new Timestamp(System.currentTimeMillis() + 10001);

System.out.println("modifiedDate timestamp: " + modifiedDate.toString());


Timestamp lastEditTimestamp = new Timestamp(System.currentTimeMillis());

System.out.println("lastEditTimestamp timestamp: " +
lastEditTimestamp.toString());

if (modifiedDate.getTime() - lastEditTimestamp.getTime() > 10000) {

System.out.println("modifiedDate more than 10,000 milliseconds after
lastEditTimestamp");

}

In _most_ but NOT all cases, this code worked fine; the difference of 10001
milliseconds is detected as being greater than 10000 milliseconds and the
message in the last println() statement is printed. But in a _few_ cases,
the if statement did not detect a sufficient difference to trigger the
message.

I seem to recall reading that timestamps in Java are not accurate to the
millisecond, let alone the nanosecond. If I remember correctly, the
precision of timestamps is only roughly 0.05 seconds. I suspect that this is
the cause of the inconsistent behaviour. Therefore, if you get your
timestamps from Java, e.g Ssytem.currentTimeMillis(), you can't
realistically expect the precision you want.

If you can't live with that, I think your only hope is to acquire your
timestamps outside of the Java where you can get better precision.

But my memory is not that great so please don't take my remarks to the bank;
I may be misremembering the precision of timestamps and there may be some
reasonable way to get sufficiently precise timestamps.

Rhino
 
R

Roedy Green

Date modifiedDate = new Date();
Date lastEditTimeStamp = (Date) curRbtMpInf
.get(rbt_map.LAST_EDIT_TIMESTAMP);

you don't want modifiedDate() since it gets set once and never
changes. You want java.lang.System.currentTimeMillis().

I have no idea what curRbtMpInf is. In any case dump out the value of
the two dates to see to make sure they are both plausible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top