鉴于两者java.time.Clock.systemDefaultZone().getZone()都java.util.TimeZone.getDefault().toZoneId()返回相同的输出,两者之间有什么区别吗?
例如这段代码
import java.time.Clock;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
System.out.println("Clock.systemDefaultZone().getZone() : "
+ Clock.systemDefaultZone().getZone());
System.out.println("TimeZone.getDefault().toZoneId() : "
+ TimeZone.getDefault().toZoneId());
}
}
Run Code Online (Sandbox Code Playgroud)
返回此输出
Clock.systemDefaultZone().getZone() : Europe/Paris
TimeZone.getDefault().toZoneId() : Europe/Paris
Run Code Online (Sandbox Code Playgroud) 我的 Java Bean 中有一个字段,如下所示,其中@JsonFormat包含 jackson 注释:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm")
private Timestamp createdOn;
Run Code Online (Sandbox Code Playgroud)
当我返回 时Timestamp,它总是在我的 JSON 响应中转换为 UTC 时间。让我们说格式化我的时间戳,我得到2017-09-13 15:30但我的响应被返回为2017-09-13 10:00.
我知道这是因为 Jackson 注释默认采用 System TimeZone 并将时间戳转换为 UTC。(在我的情况下,服务器属于Asia/Calcutta时区,因此偏移量是+05:30,杰克逊映射器减去 5 小时 30 分钟以将时间戳转换为 UTC)
有没有办法按原样返回时间戳,即,2017-09-13 15:30而不是2017-09-13 10:00?