Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Comparint timestamps

Thread view: 
sconeek - 29 Nov 2005 04:43 GMT
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.
Roedy Green - 29 Nov 2005 04:52 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

sconeek - 29 Nov 2005 04:58 GMT
my bad.
Date modifiedDate = new Date();
Date lastEditTimeStamp = (Date) curRbtMpInf
                    .get(rbt_map.LAST_EDIT_TIMESTAMP);

hope this helps.
Roedy Green - 29 Nov 2005 06:15 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Rhino - 29 Nov 2005 05:34 GMT
> Hi all,
>
[quoted text clipped - 17 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.