Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get difference between two DateTimes as a DateTime object?

Tags:

java

jodatime

I want to get the difference between two DateTime objects as a DateTime and not as a Period or something like that. How can I do that ?

like image 261
david blaine Avatar asked Jul 10 '26 18:07

david blaine


1 Answers

You can't, because it doesn't make sense.

What's the difference between September 25th 2012 and December 25th 2012? It's two months, or ~60 days (I haven't checked exactly) - but it certainly isn't "February 20th 1970" or anything like that.

If you find yourself wanting the difference as a DateTime, it means something in your design is messed up, and you should revisit it. If you have trouble working out exactly where the problem is, you can give us more information and we may be able to identify where the types are wrong, but fundamentally what you're asking for won't work.

like image 137
Jon Skeet Avatar answered Jul 13 '26 10:07

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!