I want to know whether the Time values of a Calendar object equal the value of a java.sql.Time object.
E.g
Calendar c; //c.getTime().toString() == "Sat Jan 07 09:00:00 GMT 2012"
Time t; //d.toString() == "09:00:00";
I tried
t.equals(c.getTime())
But because the Calendar has Date information the expression is false.
What would be the best way the compare the two?
Edit:
The Time object is retrieve though Hibernate and come with no date information.
The Calendar object is create by
Calendar c= Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 9);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
The way you use is perfectly fine. The goal is unclear, though. Why do you want
cto be equal tod?Additionally, there’s no way to have
d.toString() == "09:00:00"—Datealways have, well, the date included.What’s more important, though, is that
Datehas no timezone information (well, it used to have, but you’re discouraged to touch this part ofDate), so you cannot tell09:00 UTCfrom10:00 BST—that is, unless you specify the timezone. You can get the timezone fromCalendar c, and it sort of explains what you need to do:Calendarfrom your dateCalendarfields which are of interest for you. I suppose that will be hour, minute, second, and, perhaps, millisecond.Update: now that you’ve mentioned it’s actually
java.sql.Time, I’m worried. The problem is,java.sql.Timestores time as milliseconds since “zero epoch” value of January 1, 1970. The date part is usually stripped to January 1, 1970 — but this class does not contain timezone information. (Well, again, it sort of does, but it’s deprecated.)Calendarhas an explicitly set timezoneWhat it means in practice is, that the time from the server gets converted into milliseconds using system default timezone, then you read this value and compare it with a
Calendarwith its own timezone.If it sounds confusing and fragile, that’s because it is. So basically you have three timezones:
All three must be the same so that any comparison would make any sense.