Is there any way, to get the difference between two datetimes in seconds?
For example
First datetime: 2022-04-25 12:09:10
Second datetime: 2022-05-24 02:46:21
Is there any way, to get the difference between two datetimes in seconds?
For example
First datetime: 2022-04-25 12:09:10
Second datetime: 2022-05-24 02:46:21
2
Answers
If your date types are in the
java.time
package, in other words, are inheritors ofTemporal
: Use theChronoUnit
class.Note that this can result in a negative value, so do take its absolute value (
abs
) if necessary.There is a dedicated class for that – Duration (the same in Android doc).
Here is example usage: