Results 1 to 8 of 8

Thread: time comparing

  1. #1
    cucucur is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default time comparing

    hi! I can't understand why it doesn't work!

    PHP Code:
    String query =  "SELECT scheduleInitialTime, scheduleFinalTime from clientSchedule where clientID = 3";
    Statement s = conn.createStatement();
    ResultSet rs = s.executeQuery(query);   
    while (rs.next()) {
           		scheduleInitialTime = rs.getTime(1);
           		scheduleFinalTime = rs.getTime(2);
    }
    
    		
    Date today = new Date();
    Time time = new Time(today.getTime());
    
    
    System.out.println("start " + scheduleInitialTime);
    System.out.println("final " + scheduleFinalTime);
    System.out.println("now " + time);
    		
    if (scheduleInitialTime.before(time)){
    		System.out.println("YES!");
    } else {
    	    	System.out.println("NO");
    } 
    		
    if (scheduleFinalTime.compareTo(time) > -1){
    		System.out.println("YES");
    	} else {
        	        System.out.println("NO");
    	    }
    And this is the some execution examples:
    Java Code:
    start 08:00:00
    final 20:30:00
    now 10:58:20
    YES
    NO
    
    start 20:00:00
    final 22:00:00
    now 10:58:37
    YES
    NO
    could you help me?? thanks!!!

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    scheduleInitialTime is before current 'time' so you get "YES" as you should do..

    scheduleFinalTime is greater than time so you get "NO" as you should do..

    What doesn't work?


    EDIT:

    Oh wait, I see the problem now. The second answer should be "YES" because >-1 would indicate equal-to/greater-than (not the other way around)

    I think it's just a matter of creating a custom comparator to ensure the program is comparing the two objects correctly.

    (the other thing that occured to me was that if scheduleInitialTime & scheduleFinalTime are also Date objects, it may be including the Date as well as the Time when it makes a comparison).


    And here's an example of a custom comparator:
    1. class AgeComparator implements Comparator{
    2. public int compare(Object emp1, Object emp2){
    3. /*
    4. * parameter are of type Object, so we have to downcast it
    5. * to Employee objects
    6. */
    7. int emp1Age = ((Employee)emp1).getAge();
    8. int emp2Age = ((Employee)emp2).getAge();
    9. if(emp1Age > emp2Age)
    10. return 1;
    11. else if(emp1Age < emp2Age)
    12. return -1;
    13. else
    14. return 0;
    15. }
    16. }
    Last edited by ozzyman; 04-28-2011 at 10:35 AM.

  3. #3
    cucucur is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    yes, my solution is quite similar, but I cant understand why the compareTo method doesn't work.

    PHP Code:
    	int scheduleInitial = Integer.valueOf(String.valueOf(scheduleInitialTime).replace(":", ""));
    		int now = Integer.valueOf(String.valueOf(time).replace(":", ""));
    		int scheduleFinal = Integer.valueOf(String.valueOf(scheduleFinalTime).replace(":", ""));
    		
    		
    		if ((scheduleInitial > now) || (scheduleFinal < now)) {
    			System.out.println("No");
    		} else {
    	    	System.out.println("yes");
    	    }
    why it doesn't work?

    Thanks!

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Thats looks completely wrong i'm afraid... replacing the formatting doesn't help order the time by time.

    A better way to do this is to convert the time into (long) milliseconds, compare the millis and return int 0, -1 or 1 depending on that.

  5. #5
    cucucur is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Is it incorrect? why???

    I need to know if my service available is available now, it depends.

    I tried with java method getTime and the problem is that in the database data it starts counting 1 january 1970, this is why compareTo doesn't work.

    Could you explain to me why my way isn't correct??

    Thanks!!!

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I say it isn't correct because you used this method:

    example:

    given time is 20:20:20 (hh:mm:ss)
    remove colons --> 202020
    given another time is 05:30:00 (following day)
    remove colons --> 053000
    compare both and the latter will be passed as less-than the former


    And have you tried to use getTimeInMillis?

    Compile this program:

    Java Code:
    import java.util.Calendar;
     
    public class DateExample {
        public static void main(String[] args) {
            Calendar date = Calendar.getInstance();
            date.set(1970, 1, 1, 0, 0, 0);
            System.out.println(date.getTimeInMillis());
        }
    }

    Once you've seen that result, try using 1900 instead of 1970

  7. #7
    cucucur is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Hi! I don't care about the days, just the time.

    So there is no problem!!

    Thank you so much!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 01:40 AM
  3. Replies: 6
    Last Post: 01-18-2011, 02:31 AM
  4. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  5. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •