DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Competing Consumers With Spring Boot and Hazelcast
  • Extending Java APIs: Add Missing Features Without the Hassle
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer

Trending

  • Competing Consumers With Spring Boot and Hazelcast
  • Extending Java APIs: Add Missing Features Without the Hassle
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Loop Through Date and Calendar Ranges

Groovy Goodness: Loop Through Date and Calendar Ranges

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Nov. 22, 13 · Interview
Like (0)
Save
Tweet
Share
9.78K Views

Join the DZone community and get the full member experience.

Join For Free

Groovy already adds a lot of extra methods to the Date and Calendar classes. And since Groovy 2.2 we can use the upto() method and loop over a begin date up to another date. We pass a closure that is executed for each day. We can also iterate back using the downto() method.

// We can loop from today
// to nextWeek and invoke a closure for each day.
def today = new Date().clearTime()
def nextWeek = today + 7

today.upto(nextWeek) {
    // Print day of the week.
    println it.format('EEEE')
}

println()

nextweek.downto(today) {
    prinltn it.format('EEEE')
}

When we run the code we get the following output:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday
 
Monday
Sunday
Saturday
Friday
Thursday
Wednesday
Tuesday
Monday

In the next sample we use the upto() method on the Calendar class:

// upto() also works on Calendar objects.
def to = Calendar.instance
to.set(year: 2013, month: Calendar.NOVEMBER, date: 18)

def from = Calendar.instance
from.set(year: 2013, month: Calendar.NOVEMBER, date: 13)

from.upto(to) {
    if (it[Calendar.DATE] % 2 == 0) {
        print 'Even'
    } else {
        print 'Odd'
    }
    println ' date'
}

We get the following output:

Odd date
Even date
Odd date
Even date
Odd date
Even date

Code written with Groovy 2.2.


Groovy (programming language) Calendar (Apple)

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Competing Consumers With Spring Boot and Hazelcast
  • Extending Java APIs: Add Missing Features Without the Hassle
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: