Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Difference  RSS feed

 
amit sharma
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get date from in format "Nov 4 2006 2:36PM" from stored procedure in sql server.I want to calculate the difference in minutes between 2 dates.
Is any function available in java which suited my need or i have to write my own function.
Thanks
 
Nitin Jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dmay,
I have also face this problem earlier in my project.
As per best of my knowledge there is no function provided in Java
for date difference.
You have to manually calculate this.
[ November 04, 2006: Message edited by: Nitin Jain ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi cowboyds,

even if it is called "Calendar" the java.util.Calendar class has methods to calculate with minutes.

example:


Prints something like
Sat Nov 04 18:07:04 CET 2006
Sat Nov 04 18:22:04 CET 2006
Difference: 15 minutes



For parsing your "Nov 4 2006 2:36PM" you maybe need the java.text.SimpleDateFormat class.

See API for details.


Yours,
Bu.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cal.get(Calendar.MINUTE) cannot be used to get the number of minutes between two Dates except in very limited circumstances, i.e when the Dates are less than 1 hour apart, and therefore it won't be useful to the op.
[ November 04, 2006: Message edited by: sven studde ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calculating the difference in minutes between two Date objects is very easy. You just call getTime() on both Date objects (which returns the Date as a long variable that contains the number of milliseconds since January 1, 1970) and you subtract the values. That gives you the difference between the Date objects in milliseconds. One minute contains 60 times 1000 milliseconds, so you divide the number by 60,000.

[ November 06, 2006: Message edited by: Jesper Young ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!