Java在以色列获得当前时间

ila*_*man 5 java timezone

我想在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());
Run Code Online (Sandbox Code Playgroud)

但它总是让我比以色列目前的时间减少7个小时有人知道如何在以色列实现目前的时间?

Jon*_*eet 12

您在日历中设置了时区,但您应该在自己的日历中进行设置DateFormat.此外,您应该使用Asia/Jerusalem时区名称.您根本不需要Calendar- 只需new Date()提供当前时刻:

DateFormat timeFormat = new SimpleDateFormat("HH:mm");
timeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
String curTime = timeFormat.format(new Date());
Run Code Online (Sandbox Code Playgroud)

您应该注意到耶路撒冷的时区特别难以预测(它波动很大),因此如果您的JVM使用的时区数据源已过期,则可能不准确.