Date and time
Exposed provides comprehensive support for date and time operations through three additional modules. Each module is based on a different date-time library, offering different features and type support:
Module | Based on | Use |
|---|---|---|
| Modern Kotlin-first approach, recommended for new projects. | |
Good choice when integrating with Java code or when you need Java 8 Time API compatibility. | ||
Joda-Time library | Legacy support, includes additional functions for extracting time parts (year, month, day, hour, minute, second). Consider using newer modules for new projects. |
Basic usage
Here are examples of using date and time types. The imports will vary based on your chosen module:
Supported types
Each date-time module provides its own set of types. Below are the details for each supported type across all modules:
date()Maps to database
DATE. Used for storing date values without time component.val birthDate = date("birth_date") // Using exposed-kotlin-datetime (recommended) Events.insert { it[birthDate] = LocalDate(1990, 1, 1) } // Using exposed-java-time Events.insert { it[birthDate] = LocalDate.of(1990, 1, 1) } // Using exposed-jodatime Events.insert { it[birthDate] = org.joda.time.LocalDate(1990, 1, 1) }datetime()Maps to database
DATETIME. Used for storing both date and time values.val createdAt = datetime("created_at") .defaultExpression(CurrentDateTime) // Sets current date/time when inserting new records // Using exposed-kotlin-datetime (recommended) Events.insert { it[createdAt] = Clock.System.now() .toLocalDateTime(TimeZone.UTC) } // Note: CurrentDateTime is available in all date-time modules // Using exposed-java-time Events.insert { it[createdAt] = LocalDateTime.now() } // Using exposed-jodatime Events.insert { it[createdAt] = DateTime.now() }time()Maps to database
TIME. Used for storing time values without date component.val startTime = time("start_time") // Using exposed-kotlin-datetime (recommended) Events.insert { it[startTime] = LocalTime(9, 0) // 09:00 } // Using exposed-java-time Events.insert { it[startTime] = LocalTime.of(9, 0) // 09:00 } // Using exposed-jodatime Events.insert { it[startTime] = org.joda.time.LocalTime(9, 0) // 09:00 }timestamp()Maps to database
TIMESTAMP. Used for storing both date and time values. Not supported byexposed-jodatime.val lastModified = timestamp("last_modified") // Using exposed-kotlin-datetime (recommended) Events.insert { it[lastModified] = Clock.System.now() } // Using exposed-java-time Events.insert { it[lastModified] = Instant.now() }timestampWithTimeZone()Maps to database
TIMESTAMP WITH TIME ZONE. Used for storing both date and time values while preserving timezone information. Not supported byexposed-jodatime.val scheduledAt = timestampWithTimeZone("scheduled_at") // Using exposed-kotlin-datetime (recommended) Events.insert { it[scheduledAt] = Clock.System.now().toOffsetDateTime(TimeZone.UTC) } // Using exposed-java-time Events.insert { it[scheduledAt] = OffsetDateTime.now(ZoneOffset.UTC) }