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

服务器与本地环境间Java时区转换异常问题求助

服务器与本地环境间Java时区转换异常问题求助

我最近碰到了一个服务器和本地环境之间时区转换的棘手问题,想请教下各位大佬帮忙分析下原因。我的实体类Account里有个accountCreationDate字段,类型是OffsetDateTime,先把具体情况列出来:

  • 服务器时间戳:2023-12-20T13:37:17.288582240Z[GMT]
  • 本地时区:Europe/Tirana(时区偏移量3600秒,也就是GMT+01:00)
  • 服务器时区偏移:0(GMT)
  • 数据库时区查询:执行SQL命令 SELECT DBTIMEZONE FROM DUAL; 返回结果为 "+01:00"

接下来是我写的转换代码,分为请求转换和响应转换两部分:

请求转换代码:

if (accountRequest.getAccountCreationDate() != null) {
    Instant instant = Instant.ofEpochMilli(accountRequest.getAccountCreationDate());
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
    ZonedDateTime cetDateTime = zonedDateTime.withZoneSameInstant(ZoneOffset.ofHours(1));
    OffsetDateTime offsetDateTime = cetDateTime.toOffsetDateTime();
    requestTransformer.setAccountDateCreation(offsetDateTime);
}

响应转换代码:

if (pamUserAccount.getAccountDateCreation() != null) {
    OffsetDateTime cetDateTime = pamUserAccount.getAccountDateCreation();
    LocalDateTime localDateTime = cetDateTime.toLocalDate().atStartOfDay();
    ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneOffset.ofHours(1)); // GMT+01:00
    long epochMillis = zonedDateTime.withZoneSameInstant(ZoneOffset.UTC)
        .toInstant()
        .toEpochMilli();
    response.setAccountDateCreation(epochMillis);
}

本地环境运行完全符合预期,比如时间戳1542754800000会被转换为2018-11-21T00:00+01:00的OffsetDateTime,能正常返回对应的数据行。但到了服务器上,我却得不到预期的结果,返回的是NONE,按道理应该拿到和本地一样的数据才对。

另外我特意检查了pamUserAccount.getAccountDateCreation()的值,发现两者有差异:

  • 本地环境得到的是:2018-11-21T01:00+01:00
  • 服务器环境得到的是:2018-11-21T00:00Z

有没有大佬能帮我看看哪里出问题了?

备注:内容来源于stack exchange,提问作者Dori

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 12:13:00