This discussion is archived
4 Replies Latest reply: Apr 29, 2013 12:20 PM by jschellSomeoneStoleMyAlias RSS

Time comparison problem

1005932 Newbie
Currently Being Moderated
Hello everyone

I want to compare two times. What I am doing is

java.util.Date date = new java.util.Date();
java.sql.Time cur_time = new java.sql.Time(date.getTime());

/*this is my database connectivity code from where I am getting second date that I am comparing. This is right. Don't bother it.
Connection c= ConnectionManager.getConnection();
String sql = "select t from datesheet where subjectCode=? and sessional=? and d=?";
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1,subjectCode);
ps.setString(2,sessional);
ps.setDate(3,cur_date);
ResultSet rs = ps.executeQuery();*/

if(rs.next())
{
java.sql.Time t = rs.getTime("t");

long diff = cur_time.getTime()-t.getTime();
}
Then I am converting it to seconds, minutes and hours, but the problem is time that we get from cur_time.getTime() has more digits than time that we get from t.getTime(), so it is always greater than t.getTime() even when it is not.

long seconds = (diff/1000)%60;
long minutes = (diff/60000)%60;
long hours = (diff/(60*60*1000))%24;

Thank you. Please help. Tell me the solution of this problem.
Also if you want me to refer to some date, time tutorials, then please welcome.
Thank you.
  • 1. Re: Time comparison problem
    rp0428 Guru
    Currently Being Moderated
    >
    Then I am converting it to seconds, minutes and hours, but the problem is time that we get from cur_time.getTime() has more digits than time that we get from t.getTime(), so it is always greater than t.getTime() even when it is not.
    . . .
    Tell me the solution of this problem.
    >
    The simplest solution is to prevent this from happening:
    time that we get from cur_time.getTime() has more digits than time that we get from t.getTime()
    But since it is YOU that is creating the 'more digits' the solution is: DON'T DO THAT! ;)

    This is your code that creates the reference time:
    java.util.Date date = new java.util.Date();
    java.sql.Time cur_time = new java.sql.Time(date.getTime());
    That 'date.getTime()' call gets milliseconds per the API for java.util.date
    http://docs.oracle.com/javase/6/docs/api/java/util/Date.html
    >
    long getTime()
    Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
    >
    So BEFORE you use that 'millisecond' value to create your reference 'java.sql.Time' instance just convert it to the number of 'digits' that you want. If you want seconds divide it by 1000
    java.util.Date date = new java.util.Date();
    long mySeconds = (long) (date.getTime() / 1000L); // convert the milliseconds to seconds
    java.sql.Time cur_time = new java.sql.Time(mySeconds); // now your instance will have seconds and not milliseconds
    Now you can just compare 'seconds' to 'seconds'.

    That 'simple' solution costs you one line of code and is very easy for anyone reading your code to understand.
  • 2. Re: Time comparison problem
    970894 Newbie
    Currently Being Moderated
    Converting to milliseconds will gives the solution..

    ---SKumar
  • 3. Re: Time comparison problem
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    SKumarJ wrote:
    Converting to milliseconds will gives the solution..
    No it won 't.
  • 4. Re: Time comparison problem
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    1002929 wrote:
    Tell me the solution of this problem.
    The data store stores a timestamp in a form with a precision that is specific to the database.
    Java uses timestamps with a precision that is specific to Java.

    Assuming that they are the same is a bad idea. Assuming they will remain the same is a bad idea.

    If you want an exact match then then you must first understand how the data was stored in the database.
    And it will ONLY work if the stored value was cleaned before it was stored. And even then it is a bit suspect. (Cleaned means that the value was adjusted, before storage to insure that the normal precision of the database would not be a factor when doing queries from external sources.)

    If it wasn't cleaned then you MUST use a range query. You must create a min and max value and look for values between those two.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points