【问题标题】:Java get current time in IsraelJava获取以色列当前时间
【发布时间】:2023-03-12 15:15:02
【问题描述】:

我想用 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 小时的时间,有人知道如何达到以色列当前时间吗?

【问题讨论】:

  • 尝试只使用new Date()
  • 我现在试过了,但它不起作用,也许我做的不好,你能发布修复我原始代码的代码吗?
  • TimeZones in Java的可能重复

标签: java timezone


【解决方案1】:

您在日历中设置时区,但您应该在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 使用的时区数据源已过期,则可能不准确。

【讨论】:

    【解决方案2】:

    我不确定“Israel”是否是一个有效的时区,试试“Asia/Jerusalem”,看看this post

    【讨论】:

    • “Israel”是指向“Asia/Jerusalem”的链接,这意味着它们在内部是相同的。
    【解决方案3】:

    如果您还想以您的语言显示日期和时间,请尝试使用 LocaleTimeZone

    Locale aLocale = new Locale.Builder().setLanguage("iw").setRegion("IL").build();
    DateFormat timeFormat = new SimpleDateFormat("MMM y HH:mm", aLocale);
    timeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Jerusalem"));
    String curTime = timeFormat.format(new Date());
    

    注意:这不是这个问题的答案,但如果 OP 正在进一步寻找以色列语言的解析日期,就在这里。

    【讨论】:

      【解决方案4】:

      java.time

      java.util 日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,改用modern Date-Time API*

      使用现代日期时间 API java.time 的解决方案:

      import java.time.LocalTime;
      import java.time.ZoneId;
      
      public class Main {
          public static void main(String[] args) {
              LocalTime time = LocalTime.now(ZoneId.of("Asia/Jerusalem"));
              System.out.println(time);
          }
      }
      

      样本运行的输出:

      15:06:50.207521
      

      ONLINE DEMO

      如果您希望将格式限制为HH:mm,有以下几种方法:

      1. LocalTime 截断为ChronoUnit.MINUTES
      2. DateTimeFormatterHH:mm 模式一起使用。

      演示:

      import java.time.LocalTime;
      import java.time.ZoneId;
      import java.time.format.DateTimeFormatter;
      import java.time.temporal.ChronoUnit;
      import java.util.Locale;
      
      public class Main {
          public static void main(String[] args) {
              LocalTime timeHourMinute = LocalTime.now(ZoneId.of("Asia/Jerusalem"))
                                                  .truncatedTo(ChronoUnit.MINUTES);
              System.out.println(timeHourMinute);
      
              // Alternatively
              LocalTime time = LocalTime.now(ZoneId.of("Asia/Jerusalem"));
              DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH);
              String strTime = dtf.format(time);
              System.out.println(strTime);
          }
      }
      

      样本运行的输出:

      16:01
      16:01
      

      ONLINE DEMO

      Trail: Date Time 了解有关现代日期时间 API 的更多信息。


      * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7 . 如果您正在为一个Android项目工作并且您的Android API级别仍然不符合Java-8,请检查Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-20
        • 2012-01-10
        相关资源
        最近更新 更多