Results 1 to 13 of 13
  1. #1
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Wrong time difference calculation

    hello everybody, I'm trying to calculate the time difference between two dates. here is the code

    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));
    the result should be 01:00:00 but I got 02:00:00. can somebody fix this problem?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Wrong time difference calculation

    DST?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default 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 ...
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default 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.

    ** This space for rent **

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default 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"?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default Re: Wrong time difference calculation

    Calculate the hours minutes and seconds?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Wrong time difference calculation

    when I use the TimeZone "Europe/Lissabon" the calculation is ok!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default 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.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: Wrong time difference calculation

    Quote Originally Posted by j2me64 View Post
    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"?
    Set a TimeZone without any daylight saving for your DataFormat object. (see the APIs for details). Note that it can only disply an amount of hours < 24.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Wrong time difference calculation

    Quote Originally Posted by Tolls View Post
    And if you have a time difference greater than 24 hours?
    this shouldn't never happen, since I'm calculating the duration of our jobs on the database and there a some very slow jobs but the duration is never bigger than some hours. anyway, I will code some checks to ensure that the duration will never be grater than 24.
    Last edited by j2me64; 05-30-2012 at 10:17 PM.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default 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.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: Wrong time difference calculation

    Quote Originally Posted by j2me64 View Post
    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.
    That's what we all have been trying to tell you ;-) Hours in the range 0:00:00am up to 00:59:59am are displayed as 1:mm:ss per convention. A time value is displayed, not a duration. Better display that duration yourself ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Time Difference
    By benj in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 09:01 AM
  2. Time Calculation Assistance
    By toterpunkt in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 04:46 AM
  3. Wrong time
    By OrangeDog in forum Suggestions & Feedback
    Replies: 5
    Last Post: 11-20-2009, 03:24 AM
  4. Payroll Program exits at wrong time
    By jsand2 in forum Java Applets
    Replies: 13
    Last Post: 01-26-2009, 03:10 AM
  5. What did i do wrong this time!
    By PureAwesomeness in forum New To Java
    Replies: 28
    Last Post: 01-19-2009, 11:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •