Groovy extensions for working with Java 8+ date/time types, providing convenient methods for temporal operations and transformations
—
Functionality for working with time zones, offsets, and zone-aware temporal operations including conversions, zone information retrieval, and timezone-aware datetime creation.
ZonedDateTime represents a date-time with timezone information.
/**
* Returns a ZonedDateTime that is the specified number of seconds after this date-time.
*/
ZonedDateTime plus(ZonedDateTime self, long seconds)
/**
* Returns a ZonedDateTime that is the specified number of seconds before this date-time.
*/
ZonedDateTime minus(ZonedDateTime self, long seconds)
/**
* Returns a ZonedDateTime one second after this date-time.
*/
ZonedDateTime next(ZonedDateTime self)
/**
* Returns a ZonedDateTime one second before this date-time.
*/
ZonedDateTime previous(ZonedDateTime self)
/**
* Returns a ZonedDateTime with the time portion cleared.
*/
ZonedDateTime clearTime(ZonedDateTime self)
/**
* Formats this date-time with the provided DateTimeFormatter pattern.
*/
String format(ZonedDateTime self, String pattern)
/**
* Formats this date-time in the provided, localized FormatStyle.
*/
String format(ZonedDateTime self, FormatStyle dateTimeStyle)
/**
* Formats with ISO_LOCAL_DATE_TIME and appends the zone's short name.
*/
String getDateTimeString(ZonedDateTime self)
/**
* Formats with ISO_LOCAL_DATE and appends the zone's short name.
*/
String getDateString(ZonedDateTime self)
/**
* Formats with ISO_LOCAL_TIME and appends the zone's short name.
*/
String getTimeString(ZonedDateTime self)
/**
* Converts to a java.util.Date (adjusted to system default timezone).
*/
Date toDate(ZonedDateTime self)
/**
* Converts to a java.util.Calendar with the timezone from this ZonedDateTime.
*/
Calendar toCalendar(ZonedDateTime self)Usage Examples:
import java.time.*
def nyTime = ZonedDateTime.now(ZoneId.of('America/New_York'))
// Arithmetic operations
def inOneHour = nyTime + 3600
def yesterdaySameTime = nyTime - 86400
// Time manipulation
def startOfDay = nyTime.clearTime()
// Formatting with zone information
def formatted = nyTime.format('yyyy-MM-dd HH:mm:ss VV')
def dateTime = nyTime.dateTimeString // "2024-12-25T14:30:25EST"
def dateOnly = nyTime.dateString // "2024-12-25EST"
def timeOnly = nyTime.timeString // "14:30:25EST"
// Conversion
def date = nyTime.toDate()
def calendar = nyTime.toCalendar() // Preserves timezoneZoneId represents a timezone identifier.
/**
* Converts to a java.util.TimeZone equivalent.
*/
TimeZone toTimeZone(ZoneId self)
/**
* Returns the full display name in the default locale.
*/
String getFullName(ZoneId self)
/**
* Returns the full display name in the specified locale.
*/
String getFullName(ZoneId self, Locale locale)
/**
* Returns the short display name in the default locale.
*/
String getShortName(ZoneId self)
/**
* Returns the short display name in the specified locale.
*/
String getShortName(ZoneId self, Locale locale)
/**
* Returns the current ZoneOffset for this zone.
*/
ZoneOffset getOffset(ZoneId self)
/**
* Returns the ZoneOffset for this zone at the specified instant.
*/
ZoneOffset getOffset(ZoneId self, Instant instant)
/**
* Combines this zone with a LocalDateTime to create a ZonedDateTime.
*/
ZonedDateTime leftShift(ZoneId self, LocalDateTime dateTime)Usage Examples:
import java.time.*
def nyZone = ZoneId.of('America/New_York')
def utcZone = ZoneId.of('UTC')
// Zone information
def fullName = nyZone.fullName // "Eastern Standard Time"
def shortName = nyZone.shortName // "EST"
def currentOffset = nyZone.offset // Current offset from UTC
def offsetAt = nyZone.getOffset(Instant.parse('2024-07-01T00:00:00Z'))
// Create zoned datetime
def zonedTime = nyZone << LocalDateTime.of(2024, 12, 25, 14, 30)
// Convert to legacy timezone
def timeZone = nyZone.toTimeZone()ZoneOffset represents a fixed offset from UTC.
/**
* Converts to a java.util.TimeZone (truncated to minutes).
*/
TimeZone toTimeZone(ZoneOffset self)
/**
* Returns the hours component of this offset.
*/
int getHours(ZoneOffset self)
/**
* Returns the minutes component of this offset.
*/
int getMinutes(ZoneOffset self)
/**
* Returns the seconds component of this offset.
*/
int getSeconds(ZoneOffset self)
/**
* Supports subscript operator for accessing temporal fields.
*/
long getAt(ZoneOffset self, TemporalField field)
/**
* Combines this offset with a LocalDateTime to create an OffsetDateTime.
*/
OffsetDateTime leftShift(ZoneOffset self, LocalDateTime dateTime)
/**
* Combines this offset with a LocalTime to create an OffsetTime.
*/
OffsetTime leftShift(ZoneOffset self, LocalTime time)Usage Examples:
import java.time.*
import java.time.temporal.ChronoField
def offset = ZoneOffset.ofHoursMinutesSeconds(5, 30, 45)
// Offset components
def hours = offset.hours // 5
def minutes = offset.minutes // 30
def seconds = offset.seconds // 45
def totalSeconds = offset.totalSeconds // 19845
// Field access using subscript operator
def hourField = offset[ChronoField.OFFSET_SECONDS]
// Create offset date/time
def offsetDateTime = offset << LocalDateTime.now()
def offsetTime = offset << LocalTime.of(14, 30)
// Convert to legacy timezone
def timeZone = offset.toTimeZone() // GMT+05:30/**
* Returns the ZoneOffset currently associated with the system default ZoneId.
*/
ZoneOffset systemDefault(ZoneOffset type)Usage Examples:
import java.time.*
// Get system default offset (static method)
def systemOffset = ZoneOffset.systemDefault()Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-datetime