{"id":12952,"date":"2014-04-13T17:53:27","date_gmt":"2014-04-13T12:23:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12952"},"modified":"2014-04-13T17:53:27","modified_gmt":"2014-04-13T12:23:27","slug":"iterate-through-two-distinct-dates-groovy-2-2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/iterate-through-two-distinct-dates-groovy-2-2\/","title":{"rendered":"Iterate through two distinct dates &#8211; Groovy 2.2"},"content":{"rendered":"<p>Hi everyone,<\/p>\n<p>Ever wished for a &#8216;groovier&#8217; way to iterate through two distinct dates? Well, Groovy 2.2 gives you two methods via the &#8216;DateGroovyMethods&#8217; class to do exactly that: <strong>upto()<\/strong> and <strong>downto().<\/strong><\/p>\n<p>Here is the code showing the usage:<br \/>\n[java]<br \/>\nDate startDate = new Date() &#8211; 7<br \/>\nDate endDate = new Date()<\/p>\n<p>startDate.upto(endDate) { it -&gt; \/\/ &#8216;it&#8217; &#8211; here is a Date object <\/p>\n<p>\/\/ Some business logic involving dates &#8211; for ex. build up the total amount for last 7 days&#8217; transactions<\/p>\n<p>}<\/p>\n<p>endDate.downto(startDate) { it -&gt; \/\/ this iterates through the dates but in reverse order <\/p>\n<p>\/\/ Some business logic involving dates<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>These upto() and downto() methods even work on Calendar instances as well.<\/p>\n<p>Please note that these are just the convenience methods added in Groovy 2.2. Prior to 2.2 release, we could have achieved the same behavior via Groovy Ranges i.e.<\/p>\n<p>[java]<br \/>\nDate startDate = new Date() &#8211; 7<br \/>\nDate endDate = new Date()<\/p>\n<p>(startDate..endDate).each { it -&gt;<\/p>\n<p>\/\/ Some business logic involving dates<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone, Ever wished for a &#8216;groovier&#8217; way to iterate through two distinct dates? Well, Groovy 2.2 gives you two methods via the &#8216;DateGroovyMethods&#8217; class to do exactly that: upto() and downto(). Here is the code showing the usage: [java] Date startDate = new Date() &#8211; 7 Date endDate = new Date() startDate.upto(endDate) { it [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[1386,1385,9,1384],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12952"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12952"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12952\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}