【发布时间】:2021-01-02 01:12:46
【问题描述】:
我正在尝试获取其他时区的当前时间。我为此使用了此代码:
GregorianCalendar calender = new
GregorianCalendar(TimeZone.getTimeZone("Asia/Bangkok"));
System.out.println(calender.getTime());
但是,当我运行此代码时,此代码提供 CET 中的当前时间,因为我的本地计算机中的时间为 CET。 我很迷惑。那为什么还有在构造函数中提供 TimeZone 的范围呢?
【问题讨论】:
-
当这个问题在 2015 年被问到时,现代 Java 日期和时间 API java.time 已经问世一年了。我建议你不要使用
GregorianCalendar和Date。这些课程设计不良且早已过时。更不用说在几个答案中建议的臭名昭著的DateFormat/SimpleDateFormat。见the good answer by Basil Bourque。