Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between timestamps in terms of Hours

 
Rajesh MadhanGopal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajesh MadhanGopal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and if you want differences between time zones, go to the API for java.util.TimeZone and its subclasses too.
 
Paul Clapham
Sheriff
Pie
Posts: 20757
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic