-
time comparing
hi! I can't understand why it doesn't work!
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:
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!!!
-
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:
- class AgeComparator implements Comparator{
- public int compare(Object emp1, Object emp2){
- /*
- * parameter are of type Object, so we have to downcast it
- * to Employee objects
- */
- int emp1Age = ((Employee)emp1).getAge();
- int emp2Age = ((Employee)emp2).getAge();
- if(emp1Age > emp2Age)
- return 1;
- else if(emp1Age < emp2Age)
- return -1;
- else
- return 0;
- }
- }
-
yes, my solution is quite similar, but I cant understand why the compareTo method doesn't work.
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!
-
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.
-
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!!!
-
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:
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
-
Hi! I don't care about the days, just the time.
So there is no problem!!
Thank you so much!
-
Moderation: hijack posts from M11ke deleted.