You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Java LocalDate存入PostgreSQL后变成前一天?

问题概述

创建Java LocalDate对象(例如:"2023-09-28")并保存到PostgreSQL数据库时,存储的却是前一天的日期(例如:"2023-09-27")。

  • 数据库中存储的日期始终比预期早一天
代码示例

以下是Micronaut代码:

@MappedEntity("event")
data class Event(

    @field:Id val id: UUID,

    val date: LocalDate // <===
)

@JdbcRepository(dialect = Dialect.POSTGRES)
interface EventRepository: CrudRepository<Event, UUID> {}

保存逻辑:

val date = LocalDate.parse("2023-09-28", DateTimeFormatter.ISO_LOCAL_DATE)

val newEvent = Event(id, date)
repo.save(newEvent)

日志显示发送的是正确值:

DEBUG io.micronaut.data.query - Executing SQL query: INSERT INTO "event" ("date","id") VALUES (?,?)
TRACE io.micronaut.data.query - Binding parameter at position 1 to value 2023-09-28 with data type: DATE
数据库配置

使用的驱动:org.postgresql:postgresql

  • PostgreSQL表列类型为date:
create table event
(
    id   uuid not null,
    date date not null
);
  • PostgreSQL的date类型不含时间信息
可复现示例

提供了最小可复现代码仓库

相关调研

该问题可能与pgjdbc的“LocalDate转换应忽略时区”议题相关


内容的提问来源于stack exchange,提问作者Mike R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 18:40:01