Results 1 to 13 of 13
- 05-30-2012, 10:10 AM #1
Wrong time difference calculation
hello everybody, I'm trying to calculate the time difference between two dates. here is the code
the result should be 01:00:00 but I got 02:00:00. can somebody fix this problem?Java Code:DateFormat df3 = DateFormat.getTimeInstance(DateFormat.MEDIUM); df3.setTimeZone(TimeZone.getTimeZone("Europe/Zurich")); Date dates1 = df3.parse("10:00:00"); Date dates2 = df3.parse("11:00:00"); long diff = dates2.getTime() - dates1.getTime(); System.out.println("Time Difference = "+df3.format(diff));
- 05-30-2012, 10:51 AM #2
Re: Wrong time difference calculation
DST?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-30-2012, 10:53 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Wrong time difference calculation
It doesn't print a time difference; it prints the time at 01:00:00am in your locale; daylight saving time is active so it prints 02:00:00.
kind regards,
Jos
edit: darn, too slow again ...When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-30-2012, 11:27 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Wrong time difference calculation
The date stuff in regular Java is a bit rubbish at the whole concept of durations.
Please do not ask for code as refusal often offends.
- 05-30-2012, 01:43 PM #5
Re: Wrong time difference calculation
the diff value is 3'600'000 millis and 3600 seconds are one hour. are there any hints how to get the output in the format "01:00:00"?
- 05-30-2012, 02:26 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Wrong time difference calculation
Calculate the hours minutes and seconds?
Please do not ask for code as refusal often offends.
- 05-30-2012, 02:30 PM #7
Re: Wrong time difference calculation
when I use the TimeZone "Europe/Lissabon" the calculation is ok!
- 05-30-2012, 03:00 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Wrong time difference calculation
And if you have a time difference greater than 24 hours?
Please do not ask for code as refusal often offends.
- 05-30-2012, 03:06 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Wrong time difference calculation
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-30-2012, 10:04 PM #10
Re: Wrong time difference calculation
Last edited by j2me64; 05-30-2012 at 10:17 PM.
- 05-31-2012, 04:04 AM #11
Re: Wrong time difference calculation
Like Jos said at #2, a Date isn't a duration; it's an instant in time.
I think the Joda time API has a Duration class.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-31-2012, 11:29 AM #12
Re: Wrong time difference calculation
I got an other problem: when the duration has 0 hours 12 or 24 are displayed in the output. Because my calculations are smaller than 12 hours I will keep the java solution with date. But the class Date isn't really a good choice for calculating duration.
- 05-31-2012, 12:55 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Wrong time difference calculation
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Time Difference
By benj in forum New To JavaReplies: 1Last Post: 03-07-2011, 09:01 AM -
Time Calculation Assistance
By toterpunkt in forum New To JavaReplies: 2Last Post: 02-17-2011, 04:46 AM -
Wrong time
By OrangeDog in forum Suggestions & FeedbackReplies: 5Last Post: 11-20-2009, 03:24 AM -
Payroll Program exits at wrong time
By jsand2 in forum Java AppletsReplies: 13Last Post: 01-26-2009, 03:10 AM -
What did i do wrong this time!
By PureAwesomeness in forum New To JavaReplies: 28Last Post: 01-19-2009, 11:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks