Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

timespan false?

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two date objects - I want to get the timespan between the two dates:

return ((endTime.getTime() - startTime.getTime()) / 60*1000);


but it returns for example: 13000 (even it endures only approx 1 second). So I guess, 13000 means 1,3 sec. Is my formula false?

Should I use something like the examples described here: http://code.hammerpig.com/find-time-difference-dates-java.html:

http://code.hammerpig.com/find-time-difference-dates-java.html



Why is my formula false?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code from Hammerpig looks horrible and it does strange and unnecessary stuff with timezones.

Your formula looks correct. Are you sure your numbers are correct? What do you see if you print the content of the two Date objects:

Date.getTime() returns a number measured in milliseconds. If you divide that by 60 * 1000 you get a value in minutes. So 13000 means 13000 minutes.
 
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:If you divide that by 60 * 1000 you get a value in minutes.


Ah, but Nimo doesn't divide by 60 * 1000.
This will first calculate endTime.getTime() - startTime.getTime(); the difference in milliseconds. This is then divided by 60; the difference in milliminutes. That is then multiplied by 1000. Remember, / and * have the same operator precedence, and are therefore evaluated from left to right.

To calculate the difference in minutes the following are two options:
(first divide by 60, then divide by 1000)
(really divide by 60 * 1000 by ensuring it is evaluated before the division)

These days you should check out TimeUnit though:
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello jesper,

thanks - yes that was my fault.


hello rob,

thank you!! I did not know the TimeUnit-Class. I will use it for my problem as it works like a charm:-)

thanks!!


 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, my mistake. Thanks Rob, I didn't know the TimeUnit class - learned something today!
 
Marshal
Posts: 74783
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought TimeUnit is an enum.
 
Rob Spoor
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitpickers!
 
reply
    Bookmark Topic Watch Topic
  • New Topic