Date and time types
Exposed provides comprehensive support for date and time operations through dedicated modules. Each module is based on a different date-time library, offering different features and type support.
Modules
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. | ||
Legacy support. Consider using newer modules for new projects. |
Add a dependency
Before using date and time column types or functions, add your chosen date-time module to your build file:
Basic usage
To define date and time columns, use the column functions provided by your selected date-time module. The following examples define columns for common date and time types:
Supported types
Each date-time module provides its own set of types. Select a module to see its supported types and API links:
Column type | Database type | Kotlin type | API |
|---|---|---|---|
|
| ||
|
| ||
|
| ||
|
| ||
|
| ||
|
|
Column type | Database type | Java type | API |
|---|---|---|---|
|
| ||
|
| ||
|
| ||
|
| ||
|
| ||
|
|
Column type | Database type | Joda-Time type | API |
|---|---|---|---|
|
| ||
|
| ||
|
| ||
|
|
date()
The date() column type maps to the database DATE type. It is used for storing date values without a time component:
time()
The time() column type maps to the database TIME type. It is used for storing time values without a date component.
datetime()
The datetime() column type maps to the database DATETIME type. It is used for storing both date and time values.
The following example sets the current date/time when inserting new records:
timestamp()
The timestamp() column type maps to the database TIMESTAMP type. It is used for storing both date and time values.
timestampWithTimeZone()
The timestampWithTimeZone() column type maps to the database TIMESTAMP WITH TIME ZONE type. It is used for storing both date and time values while preserving timezone information.
duration()
The duration() column type maps to the database BIGINT type. It is used for storing the length of time between one instant and another.
Working with date and time expressions
Extracting date and time components
Exposed provides extension functions for extracting individual components from date and time expressions. These functions return SQL expressions that can be used in queries, including SELECT, WHERE, GROUP BY, and ORDER BY clauses.
The following functions are available for the date and time types supported by each Exposed date-time module:
For example, the following query selects all events that start in June:
Using the current date and time
Exposed provides the following expressions for retrieving the current date and time from the database server:
CurrentDateReturns the current date as
kotlinx.datetime.LocalDate.CurrentDateTimeReturns the current date and time as
kotlinx.datetime.LocalDateTime.CurrentTimestampReturns the current date and time as
kotlin.time.Instant.CurrentTimestampWithTimeZoneReturns the current date and time with timezone as
java.time.OffsetDateTime.
CurrentDateReturns the current date as
java.time.LocalDate.CurrentDateTimeReturns the current date and time as
java.time.LocalDateTime.CurrentTimestampReturns the current date and time as
java.time.Instant.CurrentTimestampWithTimeZoneReturns the current date and time with timezone as
java.time.OffsetDateTime.
CurrentDateReturns the current date as
org.joda.time.DateTime.CurrentDateTimeReturns the current date and time as
org.joda.time.DateTime.
For example, these expressions can be used as default expressions when defining table columns:
The object CurrentDate defines the SQL function CURRENT_DATE as the column default on table creation. The exact date function used may differ for specific database dialects.
The database evaluates these expressions when the SQL statement is executed. As a result, the returned values reflect the database server's current date and time.