• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GregorianCalendar question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a program to show the difference of hours between two dates. However, it is strange that I couldn't get it correctly in certain month.
for example:
GregorianCalendar t1 = new GregorianCalendar();
GregorianCalendar t2 = new GregorianCalendar();
t1.set(2004, Calendar.APIRL, 1, 12 ,0, 0);
t2.set(2004, Calendar.MAY, 1, 12 , 0, 0);
the output is 719 hours.
But if I put it like this
GregorianCalendar t1 = new GregorianCalendar();
GregorianCalendar t2 = new GregorianCalendar();
t1.set(2004, Calendar.AUGUST , 1, 12 ,0, 0);
t2.set(2004, Calendar.SEPTEMBER , 1, 12 , 0, 0);
the output is 720 hours.
below is the function I use to calculate the hours
public static long getHoursDiff(Date t1, Date t2)
{
long st = t1.getTime();
long et = t2.getTime();
long timeDiff = (et - st) / 1000;
long hours = timeDiff / 3600;
return Math.round(hours);
}
calling the function as getHoursDiff(t1.getTime(), t2.getTime());
could any one tell me why? Thanks in advance!
kevin
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long timeDiff = (et - st) / 1000;
long hours = timeDiff / 3600;
In each of these lines you're doing division of two integers. In java, integer division rounds towards zero - meaning for example that 1999/1000 evaluates to 1, rather than 1.999 or 2. This happens before your Math.round() call; you've already lost the data at that point. Instead try:
long timeDiff = (et - st) / 1000.0;
long hours = timeDiff / 3600.0;
As long as at least one operand is a floating-point rather than integer, Java will use floating-point division, whihc should yield the results you'd expect. (Well, with some funny rounding sometimes, but that's another discussion...)
 
kevin ou
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jim:
i modify the code like this
public static long getHoursDiff(Date t1, Date t2)
{
long st = t1.getTime();
long et = t2.getTime();
double timeDiff = (et - st) / 1000.00;
double hours = timeDiff / 3600.00;
return Math.round(hours);
}
it doesn't work either. Any ideas?
kevin
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two words: daylight savings.
 
kevin ou
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great thanks!
kevin
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could it be in any way that you spelled "April" worng?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic