Skip to content

Instantly share code, notes, and snippets.

@Joxebus
Last active February 19, 2021 01:44
Show Gist options
  • Save Joxebus/a01c95ae0024eef3a0002f3ffd26e1c5 to your computer and use it in GitHub Desktop.
Save Joxebus/a01c95ae0024eef3a0002f3ffd26e1c5 to your computer and use it in GitHub Desktop.
Working with dates on Groovy
List months = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio",
"Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"]
def today = Date.parse("dd-MM-yyyy", "3-11-2020")
def clonedDate = today.clone()
def yesterday = today - 1
def tomorrow = today + 1
clonedDate++
assert clonedDate == tomorrow
clonedDate--
assert clonedDate == today
assert today.after(yesterday)
assert today.before(tomorrow)
assert today[Calendar.YEAR] == 2020
assert today[Calendar.MONTH] == 10
assert today[Calendar.DATE] == 3
println """
Yesterday: $yesterday
Today: $today
Tomorrow: $tomorrow
"""
println "Hoy es ${today[Calendar.DATE]} de ${months[today[Calendar.MONTH]]} de ${today[Calendar.YEAR]}"
String dateFormat = "dd/MM/yyyy"
println """
Day before: ${yesterday.format(dateFormat)}
Initial: ${today.format(dateFormat)}
Day after: ${tomorrow.format(dateFormat)}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment