This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

timespan false?

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Ah, my mistake. Thanks Rob, I didn't know the TimeUnit class - learned something today!
 
Marshal
Posts: 65100
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought TimeUnit is an enum.
 
Rob Spoor
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Nitpickers!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!