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.
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.
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.
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.
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.
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
- Use java.time:
LocalDateandLocalDateTimeare immutable and avoid the time zone surprises ofDate. - Parse at the boundary: Convert input strings to dates once, at the edge of your code, and pass date objects inward.
- Fix dates in tests: Use
LocalDate.ofrather thannow()so results do not change over time. - Name your formats: Keep patterns in constants so the same format is used for reading and writing.
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
List all Groovy tutorials.