Skip to content

API Reference

/

Data types

Datetime

SurrealDB datetime values map to Java's java.time.ZonedDateTime. The SDK handles conversion automatically when deserialising query results into Value objects or POJOs.

Checks if the value is a datetime.

Method Syntax
value.isDateTime()

Returns: boolean

Returns the datetime as a ZonedDateTime.

Method Syntax
value.getDateTime()

Returns: java.time.ZonedDateTime

When using typed methods, datetime fields in your POJO should be declared as ZonedDateTime for reads unless you need a narrower type (Instant, OffsetDateTime, or LocalDateTime). When writing records, you can also use those types plus java.util.Date - see Class converters.

import java.time.ZonedDateTime;

public class Event {
    public RecordId id;
    public String title;
    public ZonedDateTime createdAt;

    public Event() {}
}

Optional<Event> event = db.select(Event.class, new RecordId("event", "conf"));
ZonedDateTime when = event.get().createdAt;
Working with datetime values
import com.surrealdb.Surreal;
import com.surrealdb.Response;
import com.surrealdb.Value;
import com.surrealdb.signin.RootCredential;
import java.time.ZonedDateTime;

try (Surreal db = new Surreal()) {
    db.connect("ws://localhost:8000");
    db.useNs("surrealdb").useDb("docs");
    db.signin(new RootCredential("root", "root"));

    Response response = db.query("RETURN time::now()");
    Value result = response.take(0);

    if (result.isDateTime()) {
        ZonedDateTime now = result.getDateTime();
    }
}

Was this page helpful?