我想用 java 获取以色列的当前时间这是我的代码:
TimeZone timeZone = TimeZone.getTimeZone("Israel");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(timeZone);
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
String curTime=timeFormat.format(calendar.getTime());
但它总是让我比以色列的当前时间少 7 个小时有人知道如何实现以色列的当前时间吗?
最佳答案
您正在日历 中设置时区,但您应该在DateFormat
中进行设置。此外,您应该使用 Asia/Jerusalem
作为时区名称。您根本不需要 Calendar
- 只需 new Date()
提供当前时刻:
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
timeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
String curTime = timeFormat.format(new Date());
您应该注意,耶路撒冷的时区特别难以预测(波动很大),因此如果您的 JVM 使用的时区数据源已过时,则可能不准确。
关于Java获取以色列当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585347/