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);
I had to do this today and the answers in this post helped my solve my problem. I know all my timezones are the same like the OPs. And I don't have the liberty to use Joda time in my legacy code so for the benefit of others who have the same conditions, here is how I did it with vanilla Java.
Methodology:
java.sql.Time
has agetTime()
due to inheritance fromjava.util.Date
. Using this method, one can create ajava.util.Date
object that represents just the time portion since Java epoch.java.util.Calendar
object to produce ajava.util.Date
object that represents another time since Java epoch.Without further adieu, here is the code:
The above produces the following output:
Using the above methodology, and by changing
.equals()
to.before()
or.after()
, various time comparison convenience methods can be created.You can use
Date
,Calendar
,GregorianCalendar
,
SimpleDateFormat` etc classes to deal with date-time in Java. Let's see some examples.Would display the current date based on the current millisecond.
The JodaTime's variant:
Since, you tagged this Question with DateTime, i assume you use Joda already
?
why don't you compare the time in milliseconds?
The way you use is perfectly fine. The goal is unclear, though. Why do you want
c
to be equal tod
?Additionally, there's no way to have
d.toString() == "09:00:00"
—Date
always have, well, the date included.What's more important, though, is that
Date
has no timezone information (well, it used to have, but you're discouraged to touch this part ofDate
), so you cannot tell09:00 UTC
from10: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:Calendar
from your dateCalendar
fields 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.Time
stores 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.)Calendar
has 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
Calendar
with 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.