我的服务器和本地环境之间的时区转换遇到问题。我有一个实体帐户,其字段 accountCreationDate 为 OffsetDateTime。详细信息如下:
在我的代码中,我尝试在服务器和本地环境之间转换时间戳:
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);
}
Run Code Online (Sandbox Code Playgroud)
响应变压器:
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);
Run Code Online (Sandbox Code Playgroud)
本地工作正常,时间戳为 1542754800000,转换为 2018-11-21T00:00+01:00 OffsetDateTime 并返回结果。但是,在服务器上,我什么也没有得到。我希望收到同一行数据。对这里可能出了什么问题有什么见解吗?
我也检查了一下:
pamUserAccount.getAccountDateCreation();
Run Code Online (Sandbox Code Playgroud)
本地我得到:2018-11-21T01:00+01:00
在服务器中我得到:2018-11-21T00:00Z