ZetCode

Groovy Dates

last modified October 15, 2026

Groovy works with the standard Java date and time API. For new code, use the java.time classes such as LocalDate and LocalDateTime. This tutorial also shows the legacy java.util.Date methods, which you will still meet in older code. It covers creating, parsing, formatting, and calculating with dates.

Creating Dates

LocalDate.of creates a date from its parts. Months and days are ordinary numbers, and the class prints in ISO format, yyyy-MM-dd.

CreateDate.groovy
import java.time.LocalDate
import java.time.LocalDateTime

def d = LocalDate.of(2026, 10, 15)
println d

def dt = LocalDateTime.of(2026, 10, 15, 14, 30)
println dt

d prints as 2026-10-15. LocalDateTime adds the time of day. Both classes are immutable, so methods return new values.

Parsing and Formatting

parse reads an ISO string, while DateTimeFormatter handles other patterns in both directions.

ParseFormat.groovy
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

def d = LocalDate.parse("2027-01-01")
println d

def dt = LocalDateTime.of(2026, 10, 15, 14, 30)
println dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
println d.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))

The pattern letters follow Java conventions: yyyy is the year, MM the month, dd the day, and HH the 24-hour hour. Use the same formatter to parse text with a custom layout.

Date Arithmetic

Methods such as plusDays, minusMonths, and plusYears return a new date. ChronoUnit measures the gap between two dates.

DateArithmetic.groovy
import java.time.LocalDate
import java.time.temporal.ChronoUnit

def start = LocalDate.of(2026, 10, 15)
def end = LocalDate.parse("2027-01-01")

println start.plusDays(30)
println start.minusMonths(2)
println ChronoUnit.DAYS.between(start, end)
println start.isBefore(end)

ChronoUnit.DAYS.between returns the number of days from start to end, which is 78 here. The comparison method isBefore is clearer than comparing values directly.

Day of Week and Current Date

The dayOfWeek property returns a DayOfWeek enum value. Use LocalDate.now() to read the system date, which changes from run to run, so tests should use a fixed date instead.

DayOfWeek.groovy
import java.time.LocalDate

def d = LocalDate.of(2026, 10, 15)
println d.dayOfWeek
println d.dayOfWeek.value

println LocalDate.now().year >= 2026

d.dayOfWeek prints THURSDAY, and its numeric value is 4 (Monday is 1). The last line reads the current year from the system clock.

Legacy java.util.Date

Groovy adds format and Date.parse helpers to java.util.Date. Prefer java.time in new code, but these helpers are useful when an API returns a Date.

LegacyDate.groovy
def legacy = Date.parse("yyyy-MM-dd", "2026-10-15")
println legacy.format("dd/MM/yyyy")

println new Date().format("yyyy") as Integer >= 2026

Date.parse converts the string with the given pattern, and format prints the date with another. The Date class uses the system default time zone, so results can differ across machines.

Best Practices

Source

java.time Package Documentation | Groovy Development Kit

With java.time and a few Groovy helpers, date handling stays short and predictable. Create dates from fixed values, format them explicitly, and use ChronoUnit for differences.

Author

My name is Jan Bodnar, and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Groovy tutorials.