Possible Duplicate:
How to calculate time difference in java?
Actually I want to subtract two dates in Java. I tried a lot but can't succeed. So anyone please help me.
Date dtStartDate=pCycMan.getStartDate();
Date dtEndDate=pCycMan.getEndDate();
How can I subtract these two Dates?
long msDiff = dtEndDate.getTime() - dtStartDate.getTime()
msDiff
now holds the number of milliseconds difference between the two dates. Feel free to use the division and mod operators to convert to other units.
how can i subtract these two Dates?
You can get the difference in milliseconds like this:
dtEndDate.getTime() - dtStartDate.getTime()
(getTime
returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.)
However, for this type of date arithmetics the Joda time library, which has proper classes for time durations (Duration
) is probably a better option.
To substract to dates you can get them both as longs getTime()
and subtract the values that are returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With