{"id":36,"date":"2004-08-13T22:20:23","date_gmt":"2004-08-13T21:20:23","guid":{"rendered":"http:\/\/www.paullynch.org\/blog\/?p=36"},"modified":"2004-08-13T22:20:23","modified_gmt":"2004-08-13T21:20:23","slug":"java-date-arithmetic","status":"publish","type":"post","link":"http:\/\/www.paullynch.org\/blog\/?p=36","title":{"rendered":"Java date arithmetic"},"content":{"rendered":"<p>I am used to using the old (deprecated) WebObjects NSTimestamp class to do basic arithmetic on dates; it was easy with one method call to add an arbitrary number of days\/weeks and get the correct date, or even to calculate the difference between two dates. <!-- more --> In the new (release 5.0, so not that new) world of WebObjects, these methods are deprecated, although they do still exist. I see that NSTimestamp is a subclass of Date, so that lets us into the world of pure Java. The documentation points you to GregorianCalendar for further edification. A bit of scanning around shows me that Date has an add() method; for differences, use this: <code> double days = (toDate.getTime()-fromDate.getTime())\/1000\/60\/60\/24; <\/code> Seems very crude to me. It will calculate a date interval in days, meaning 24 hour periods rather than midnight transitions. It will also round down, courtesy of the cast to (double), which is necessary to prevent a &#8220;loss of precision&#8221; compiler error.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am used to using the old (deprecated) WebObjects NSTimestamp class to do basic arithmetic on dates; it was easy with one method call to add an arbitrary number of days\/weeks and get the correct date, or even to calculate the difference between two dates. In the new (release 5.0, so not that new) world [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[23,38],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-programming-for-apples","tag-java","tag-webobjects"],"_links":{"self":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":0,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.paullynch.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}