Forums Register Login

Difference between timestamps in terms of Hours

+Pie Number of slices to send: Send
Hi

I need to get the difference between 2 dates in terms of hours , i tried getting the difference in milli seconds and converting them into Hours and it seems to fail in certain cases.

could some one help me out with this.

please find below the code i used :


public long dateDiff() {

Date dateA = new Date();

TimeZone t = TimeZone.getTimeZone("CST");
TimeZone.setDefault(t);

Date dateB = new Date();

java.util.Calendar c1 = Calendar.getInstance();
java.util.Calendar c2 = Calendar.getInstance();
long time1;
long time2;
long diff;

c1.setTime(dateA);
c2.setTime(dateB);
time1 = c1.getTime().getTime();
time2 = c2.getTime().getTime();
diff = time1 - time2;
long diffhours = diff/(3600000);
return diffhours;
}
+Pie Number of slices to send: Send
What sort of failure do you get? I have never tried that method before, but just looking at it for 1 second, I can see only one problem.

Because you are using integer arithmetic, if you have 3590000 milliseconds, you will get a difference of 0 hours.
+Pie Number of slices to send: Send
The failure is that , wen i run it

DateA would have IST timestamp and DateB would have CST time stamp.

ideally the difference in hours should be 10.5 hrs , but the difference comes to only 15 milli seconds.

i know am doing some thing wrong here, but not able to get thru it.
+Pie Number of slices to send: Send
 

Originally posted by Rajesh MadhanGopal:
DateA would have IST timestamp and DateB would have CST time stamp.


No, there's a mistake. Class Date doesn't know anything about time zones. It just stores a number of milliseconds since 1 January 1970 in the GMT timezone (see the API documentation of class java.util.Date).

Class Calendar does know about time zones - it has methods setTimeZone(...) and getTimeZone() to set and get the time zone. Note that if you use setTime(...) and getTime() on a Calendar, to set the time using a Date object or get the time as a Date object, those Date objects will always be in GMT, no matter what you set the time zone of the Calendar object to.

If you want to see what the time of a Calendar object is set to in the time zone of the Calendar, try this:

[ August 17, 2006: Message edited by: Jesper Young ]
+Pie Number of slices to send: Send
 

No, there's a mistake. Class Date doesn't know anything about time zones. It just stores a number of milliseconds since 1 January 1970 in the GMT timezone (see the API documentation of class java.util.Date).



since my JVM resides in IST , wen i instantiate a date object , the timestamp would be of IST. When i set the time zone to CST and instantiate another date object after setting it to CST , it gives me the CST timestamp.

i need to get the difference between them so as to get the differnce in hours between the 2 time zones , its here where am facing problems.
+Pie Number of slices to send: Send
. . . and if you want differences between time zones, go to the API for java.util.TimeZone and its subclasses too.
+Pie Number of slices to send: Send
 

Originally posted by Rajesh MadhanGopal:
since my JVM resides in IST , wen i instantiate a date object , the timestamp would be of IST. When i set the time zone to CST and instantiate another date object after setting it to CST , it gives me the CST timestamp.

i need to get the difference between them so as to get the differnce in hours between the 2 time zones , its here where am facing problems.

Well, no. As Jesper already said, a Date object doesn't have a timezone. Phrases like "It gives me the CST timestamp" are too vague to work with. Why don't you post the code you have already? Chances are that by deleting some of it we should be able to get it working.
+Pie Number of slices to send: Send
You can try the following, which will do the trick.

Calendar aCSTCalendar = Calendar.getInstance();
Calendar anISTCalendar = Calendar.getInstance();

aCSTCalendar.setTimeZone(TimeZone.getTimeZone("CST"));
anISTCalendar.setTimeZone(TimeZone.getTimeZone("IST"));

return anISTCalendar.get(Calendar.HOUR_OF_DAY) - aCSTCalendar.get(Calendar.HOUR_OF_DAY);

or

return aCSTCalendar.get(Calendar.HOUR_OF_DAY) - anISTCalendar.get(Calendar.HOUR_OF_DAY);

depending on which one you want
+Pie Number of slices to send: Send
To add to Brian's post: Some time zones differ by something else than an exact number of hours (10.5 hours for example), Brian's code only looks at the hours and not at the minutes field.

Class TimeZone contains methods to get the offset of a timezone relative to GMT, as Campbell already said:
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2006 times.
Similar Threads
java.util.Date
problem with date calcaulation
about "Date" class
java.sql.Date math
Difference between 2 date in day
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:47:00.