Comparint timestamps

Discussion in 'Java' started by sconeek, Nov 29, 2005.

  1. sconeek

    sconeek Guest

    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.
    sconeek, Nov 29, 2005
    #1
    1. Advertising

  2. sconeek

    Roedy Green Guest

    On 28 Nov 2005 20:43:04 -0800, "sconeek" <> wrote,
    quoted or indirectly quoted someone who said :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 29, 2005
    #2
    1. Advertising

  3. sconeek

    sconeek Guest

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

    hope this helps.
    sconeek, Nov 29, 2005
    #3
  4. sconeek

    Rhino Guest

    "sconeek" <> wrote in message
    news:...
    > 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
    Rhino, Nov 29, 2005
    #4
  5. sconeek

    Roedy Green Guest

    On 28 Nov 2005 20:58:08 -0800, "sconeek" <> wrote,
    quoted or indirectly quoted someone who said :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 29, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chumma Dede
    Replies:
    2
    Views:
    397
    Chumma Dede
    Feb 11, 2005
  2. Craig G

    Using SQL TimeStamps in datagrid

    Craig G, Feb 28, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    439
    bruce barker
    Mar 1, 2005
  3. Replies:
    1
    Views:
    384
    Roedy Green
    Nov 30, 2005
  4. Martin Bless
    Replies:
    1
    Views:
    2,869
    Matt Gerrans
    Dec 9, 2004
  5. Replies:
    0
    Views:
    453
Loading...

Share This Page