File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Difference between timestamps in terms of Hours Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between timestamps in terms of Hours" Watch "Difference between timestamps in terms of Hours" New topic
Author

Difference between timestamps in terms of Hours

Rajesh MadhanGopal
Ranch Hand

Joined: Jan 19, 2006
Posts: 69
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;
}


Thanks<br />Rajesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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.
Rajesh MadhanGopal
Ranch Hand

Joined: Jan 19, 2006
Posts: 69
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rajesh MadhanGopal
Ranch Hand

Joined: Jan 19, 2006
Posts: 69
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
. . . and if you want differences between time zones, go to the API for java.util.TimeZone and its subclasses too.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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.
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between timestamps in terms of Hours