Groovy extensions for working with Java 8+ date/time types, providing convenient methods for temporal operations and transformations
—
Core functionality for working with dates, times, and date-time objects including arithmetic operations, formatting, navigation, and conversions.
LocalDate represents a date without time or timezone information.
/**
* Returns a LocalDate that is the specified number of days after this date.
*/
LocalDate plus(LocalDate self, long days)
/**
* Returns a LocalDate that is the specified number of days before this date.
*/
LocalDate minus(LocalDate self, long days)
/**
* Calculates the number of days between two dates.
*/
long minus(LocalDate self, LocalDate other)
/**
* Returns a LocalDate one day after this date.
*/
LocalDate next(LocalDate self)
/**
* Returns a LocalDate one day before this date.
*/
LocalDate previous(LocalDate self)
/**
* Formats this date with the provided DateTimeFormatter pattern.
*/
String format(LocalDate self, String pattern)
/**
* Formats this date in the provided, localized FormatStyle.
*/
String format(LocalDate self, FormatStyle dateStyle)
/**
* Formats this date with the ISO_LOCAL_DATE formatter.
*/
String getDateString(LocalDate self)
/**
* Combines this date with a LocalTime to create a LocalDateTime.
*/
LocalDateTime leftShift(LocalDate self, LocalTime time)
/**
* Combines this date with an OffsetTime to create an OffsetDateTime.
*/
OffsetDateTime leftShift(LocalDate self, OffsetTime time)
/**
* Returns a Period between this date (inclusive) and the provided date (exclusive).
*/
Period rightShift(LocalDate self, LocalDate other)
/**
* Converts to a java.util.Date with time portion cleared.
*/
Date toDate(LocalDate self)
/**
* Converts to a java.util.Calendar with time portion cleared.
*/
Calendar toCalendar(LocalDate self)Usage Examples:
import java.time.*
import java.time.format.FormatStyle
def today = LocalDate.now()
// Arithmetic operations
def nextWeek = today + 7
def lastMonth = today - 30
def daysBetween = nextWeek - today // Returns 7
// Navigation
def tomorrow = today.next()
def yesterday = today.previous()
// Formatting
def formatted = today.format('MM/dd/yyyy') // "12/25/2024"
def localized = today.format(FormatStyle.LONG) // "December 25, 2024"
def iso = today.dateString // "2024-12-25"
// Combining with time
def datetime = today << LocalTime.of(14, 30) // LocalDateTime
def offsetDateTime = today << OffsetTime.of(14, 30, 0, 0, ZoneOffset.ofHours(5))
// Period calculation
def period = today >> (today + 30) // Period of 30 daysLocalTime represents a time without date or timezone information.
/**
* Returns a LocalTime that is the specified number of seconds after this time.
*/
LocalTime plus(LocalTime self, long seconds)
/**
* Returns a LocalTime that is the specified number of seconds before this time.
*/
LocalTime minus(LocalTime self, long seconds)
/**
* Returns a LocalTime one second after this time.
*/
LocalTime next(LocalTime self)
/**
* Returns a LocalTime one second before this time.
*/
LocalTime previous(LocalTime self)
/**
* Formats this time with the provided DateTimeFormatter pattern.
*/
String format(LocalTime self, String pattern)
/**
* Formats this time in the provided, localized FormatStyle.
*/
String format(LocalTime self, FormatStyle timeStyle)
/**
* Formats this time with the ISO_LOCAL_TIME formatter.
*/
String getTimeString(LocalTime self)
/**
* Combines this time with a LocalDate to create a LocalDateTime.
*/
LocalDateTime leftShift(LocalTime self, LocalDate date)
/**
* Combines this time with a ZoneOffset to create an OffsetTime.
*/
OffsetTime leftShift(LocalTime self, ZoneOffset offset)
/**
* Converts to a java.util.Date (using today's date).
*/
Date toDate(LocalTime self)
/**
* Converts to a java.util.Calendar (using today's date).
*/
Calendar toCalendar(LocalTime self)Usage Examples:
import java.time.*
def now = LocalTime.now()
// Arithmetic operations
def inOneMinute = now + 60 // Add 60 seconds
def tenSecondsAgo = now - 10
// Navigation
def nextSecond = now.next()
def previousSecond = now.previous()
// Formatting
def formatted = now.format('HH:mm:ss') // "14:30:25"
def timeString = now.timeString // "14:30:25.123"
// Combining with date and offset
def datetime = now << LocalDate.of(2024, 12, 25)
def offsetTime = now << ZoneOffset.ofHours(-5)LocalDateTime represents a date-time without timezone information.
/**
* Returns a LocalDateTime that is the specified number of seconds after this date-time.
*/
LocalDateTime plus(LocalDateTime self, long seconds)
/**
* Returns a LocalDateTime that is the specified number of seconds before this date-time.
*/
LocalDateTime minus(LocalDateTime self, long seconds)
/**
* Returns a LocalDateTime one second after this date-time.
*/
LocalDateTime next(LocalDateTime self)
/**
* Returns a LocalDateTime one second before this date-time.
*/
LocalDateTime previous(LocalDateTime self)
/**
* Returns a LocalDateTime with the time portion cleared (truncated to days).
*/
LocalDateTime clearTime(LocalDateTime self)
/**
* Formats this date-time with the provided DateTimeFormatter pattern.
*/
String format(LocalDateTime self, String pattern)
/**
* Formats this date-time in the provided, localized FormatStyle.
*/
String format(LocalDateTime self, FormatStyle dateTimeStyle)
/**
* Formats this date-time with the ISO_LOCAL_DATE_TIME formatter.
*/
String getDateTimeString(LocalDateTime self)
/**
* Formats the date portion with the ISO_LOCAL_DATE formatter.
*/
String getDateString(LocalDateTime self)
/**
* Formats the time portion with the ISO_LOCAL_TIME formatter.
*/
String getTimeString(LocalDateTime self)
/**
* Combines this date-time with a ZoneOffset to create an OffsetDateTime.
*/
OffsetDateTime leftShift(LocalDateTime self, ZoneOffset offset)
/**
* Combines this date-time with a ZoneId to create a ZonedDateTime.
*/
ZonedDateTime leftShift(LocalDateTime self, ZoneId zone)
/**
* Converts to a java.util.Date.
*/
Date toDate(LocalDateTime self)
/**
* Converts to a java.util.Calendar.
*/
Calendar toCalendar(LocalDateTime self)Usage Examples:
import java.time.*
import java.time.format.FormatStyle
def now = LocalDateTime.now()
// Arithmetic operations
def inFiveMinutes = now + 300 // Add 300 seconds
def anHourAgo = now - 3600
// Time manipulation
def startOfDay = now.clearTime() // Same date at 00:00:00
// Formatting
def custom = now.format('yyyy-MM-dd HH:mm:ss')
def iso = now.dateTimeString // ISO format
def dateOnly = now.dateString // Date part only
def timeOnly = now.timeString // Time part only
// Adding timezone information
def offsetDateTime = now << ZoneOffset.ofHours(2)
def zonedDateTime = now << ZoneId.of('Europe/London')Instant represents a point in time on the UTC timeline.
/**
* Returns an Instant that is the specified number of seconds after this instant.
*/
Instant plus(Instant self, long seconds)
/**
* Returns an Instant that is the specified number of seconds before this instant.
*/
Instant minus(Instant self, long seconds)
/**
* Returns an Instant one second after this instant.
*/
Instant next(Instant self)
/**
* Returns an Instant one second before this instant.
*/
Instant previous(Instant self)
/**
* Converts to a java.util.Date.
*/
Date toDate(Instant self)
/**
* Converts to a java.util.Calendar in GMT timezone.
*/
Calendar toCalendar(Instant self)Usage Examples:
import java.time.*
def now = Instant.now()
// Arithmetic operations
def inOneMinute = now + 60
def fiveSecondsAgo = now - 5
// Navigation
def nextInstant = now.next()
def previousInstant = now.previous()
// Conversion to legacy types
def date = now.toDate()
def calendar = now.toCalendar() // GMT timezoneOffsetDateTime represents a date-time with an offset from UTC/Greenwich.
/**
* Returns an OffsetDateTime that is the specified number of seconds after this date-time.
*/
OffsetDateTime plus(OffsetDateTime self, long seconds)
/**
* Returns an OffsetDateTime that is the specified number of seconds before this date-time.
*/
OffsetDateTime minus(OffsetDateTime self, long seconds)
/**
* Returns an OffsetDateTime one second after this date-time.
*/
OffsetDateTime next(OffsetDateTime self)
/**
* Returns an OffsetDateTime one second before this date-time.
*/
OffsetDateTime previous(OffsetDateTime self)
/**
* Returns an OffsetDateTime with the time portion cleared (truncated to days).
*/
OffsetDateTime clearTime(OffsetDateTime self)
/**
* Formats this offset date-time with the provided DateTimeFormatter pattern.
*/
String format(OffsetDateTime self, String pattern)
/**
* Formats this offset date-time in the provided, localized FormatStyle.
*/
String format(OffsetDateTime self, FormatStyle dateTimeStyle)
/**
* Formats this offset date-time with the ISO_OFFSET_DATE_TIME formatter.
*/
String getDateTimeString(OffsetDateTime self)
/**
* Formats the date portion with the ISO_OFFSET_DATE formatter.
*/
String getDateString(OffsetDateTime self)
/**
* Formats the time portion with the ISO_OFFSET_TIME formatter.
*/
String getTimeString(OffsetDateTime self)
/**
* Converts to a java.util.Date.
*/
Date toDate(OffsetDateTime self)
/**
* Converts to a java.util.Calendar.
*/
Calendar toCalendar(OffsetDateTime self)Usage Examples:
import java.time.*
import java.time.format.FormatStyle
def offsetDateTime = OffsetDateTime.now()
// Arithmetic operations
def inTenMinutes = offsetDateTime + 600 // Add 600 seconds
def anHourAgo = offsetDateTime - 3600
// Time manipulation
def startOfDay = offsetDateTime.clearTime() // Same date at 00:00:00 with same offset
// Formatting
def custom = offsetDateTime.format('yyyy-MM-dd HH:mm:ss XXX')
def iso = offsetDateTime.dateTimeString // ISO format with offset
def dateOnly = offsetDateTime.dateString // Date part with offset
def timeOnly = offsetDateTime.timeString // Time part with offset
// Conversion to legacy types
def date = offsetDateTime.toDate()
def calendar = offsetDateTime.toCalendar()OffsetTime represents a time with an offset from UTC/Greenwich.
/**
* Returns an OffsetTime that is the specified number of seconds after this time.
*/
OffsetTime plus(OffsetTime self, long seconds)
/**
* Returns an OffsetTime that is the specified number of seconds before this time.
*/
OffsetTime minus(OffsetTime self, long seconds)
/**
* Returns an OffsetTime one second after this time.
*/
OffsetTime next(OffsetTime self)
/**
* Returns an OffsetTime one second before this time.
*/
OffsetTime previous(OffsetTime self)
/**
* Formats this offset time with the provided DateTimeFormatter pattern.
*/
String format(OffsetTime self, String pattern)
/**
* Formats this offset time in the provided, localized FormatStyle.
*/
String format(OffsetTime self, FormatStyle timeStyle)
/**
* Formats this offset time with the ISO_OFFSET_TIME formatter.
*/
String getTimeString(OffsetTime self)
/**
* Combines this offset time with a LocalDate to create an OffsetDateTime.
*/
OffsetDateTime leftShift(OffsetTime self, LocalDate date)
/**
* Converts to a java.util.Date (using today's date).
*/
Date toDate(OffsetTime self)
/**
* Converts to a java.util.Calendar (using today's date).
*/
Calendar toCalendar(OffsetTime self)Usage Examples:
import java.time.*
def offsetTime = OffsetTime.now()
// Arithmetic operations
def inFiveMinutes = offsetTime + 300 // Add 300 seconds
def tenSecondsAgo = offsetTime - 10
// Navigation
def nextSecond = offsetTime.next()
def previousSecond = offsetTime.previous()
// Formatting
def formatted = offsetTime.format('HH:mm:ss XXX') // "14:30:25 +05:00"
def timeString = offsetTime.timeString // ISO format with offset
// Combining with date
def offsetDateTime = offsetTime << LocalDate.of(2024, 12, 25)
// Conversion to legacy types
def date = offsetTime.toDate()
def calendar = offsetTime.toCalendar()Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-datetime