aspose file tools
The moose likes Beginning Java and the fly likes Date Difference Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Date Difference" Watch "Date Difference" New topic
Author

Date Difference

amit sharma
Ranch Hand

Joined: Jul 19, 2006
Posts: 129
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

Joined: Oct 24, 2006
Posts: 4
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
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
Bartender

Joined: Aug 16, 2005
Posts: 12952
    
    3

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Date Difference
 
Similar Threads
Different results using different JDK versions
Regarding Ajax Response
How to find the difference of a day in Oracle
How to get difference between dates
Java.sql.Date