Java’s LocalDateTime and PostgreSQL’s time types “aren’t talking about the same kind of time.”


First, get clear on PostgreSQL’s two time types

TIMESTAMP           → without time zone, just a bare time "2026-06-25 10:00:00"
TIMESTAMPTZ         → with time zone, stored internally as UTC, converted to the session time zone on query

On the Java side

LocalDateTime       → no concept of a time zone, just a bare time
ZonedDateTime       → with time zone
OffsetDateTime      → with an offset (e.g. +08:00)
Instant             → a UTC timestamp

Why the error happens

The PostgreSQL JDBC driver (especially newer versions 42.x+) is very strict about type matching:

flowchart TD
    A[Java LocalDateTime] --> B[JDBC driver]
    B --> C{PostgreSQL column type}
    C -->|TIMESTAMP| D[✅ can match]
    C -->|TIMESTAMPTZ| E[❌ type mismatch error]

If your column is TIMESTAMPTZ (with time zone) but Java passes a LocalDateTime (without time zone), the driver doesn’t know which time zone to use for conversion, so it rejects it outright.


Three common errors

Cannot convert LocalDateTime to TIMESTAMPTZ
Bad value for type timestamp/date
column is of type timestamp with time zone but expression is of type timestamp

Solutions

Option 1: Change the Java type (recommended)

// Change the field in the entity class to
private OffsetDateTime createdAt;
// or
private Instant createdAt;

OffsetDateTime naturally matches PostgreSQL’s TIMESTAMPTZ.

Option 2: Change the PostgreSQL column type

-- If you don't need a time zone, change the column to without time zone
ALTER TABLE tb_archive 
ALTER COLUMN created_at TYPE TIMESTAMP;

Then LocalDateTime works fine.

Option 3: Force a type conversion (a stopgap, not recommended)

// Convert manually at the MyBatis layer
createdAt.atOffset(ZoneOffset.UTC)

The simplest advice

flowchart TD
    A[Your scenario] --> B{Need a time zone?}
    B -->|Yes, e.g. users in multiple countries| C[PostgreSQL: TIMESTAMPTZ\nJava: OffsetDateTime]
    B -->|No, single-time-zone project| D[PostgreSQL: TIMESTAMP\nJava: LocalDateTime]