java - java中的几个时区没有设置偏移量

标签 java jakarta-ee calendar

我想从用户那里获取时间和时区并创建一个条目。使用下面的日历 API 来执行此操作,它适用于几个时区而不适用于几个时区

calendar.setTime(eventFormEntryBean.getStartDate());
TimeZone timeZone = TimeZone.getTimeZone("Europe/Amsterdam");
calendar.setTimeZone(timeZone);

工作时区(最后+xx:xx)

  • 太平洋/帕劳 2019-11-27T20:51:09.000+09:00
  • IST - 2019-11-20T22:00:00.000+05:30
  • 欧洲/阿姆斯特丹 - 2019-11-28T12:49:24.000+01:00
  • 美国/洛杉矶 - 2019-11-20T21:32:49.000-08:00

不工作时区:-

  • 非洲/达喀尔 - 2019-11-21T05:30:45.000Z
  • 伦敦(欧洲/伦敦)- 2019-11-21T12:08:42.000Z

对于上述伦敦和非洲/达喀尔时区,没有任何区分时区的标志,只是在末尾注明“.000Z”。是否需要设置任何属性才能获得完整时区? .000z 是什么意思?

最佳答案

如果您希望能够编写反射(reflect)偏移量和时区之间差异的代码,请离开 java.util 并切换到 java.time(对于 Java 8 + 和 support library for Java 6 and 7 )。

然后你可以做这样的事情:

public static void main(String[] args) {
    /*
     * the base of this example is a date time with an offset of +01:00
     * (which is present in several zones, not just in Europe/Amsterdam!)
     */
    String datetime = "2019-11-28T12:49:24.000+01:00";
    // parse it to an offset-aware object
    OffsetDateTime plusOneHourOffsetDateTime = OffsetDateTime.parse(datetime);
    // print it to be sure ;-)
    System.out.println(plusOneHourOffsetDateTime
            .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    // convert it to a zone-aware date time object by providing the zone
    ZonedDateTime europeAmsterdamZonedDateTime = plusOneHourOffsetDateTime
            .atZoneSameInstant(ZoneId.of("Europe/Amsterdam"));
    // print it
    System.out.println(europeAmsterdamZonedDateTime
            .format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // then take the same instant but use a different time zone
    ZonedDateTime utcZonedDateTime = plusOneHourOffsetDateTime
            .atZoneSameInstant(ZoneId.of("UTC"));
    // print that, it adds a Z (indicating an offset of 00:00) and the time zone
    // that was specified
    System.out.println(utcZonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // take a totally different time zone and do it again
    ZonedDateTime pacificPalauZonedDateTime = plusOneHourOffsetDateTime
            .atZoneSameInstant(ZoneId.of("Pacific/Palau"));
    // print that one, too
    System.out.println(pacificPalauZonedDateTime
            .format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}

输出这个

2019-11-28T12:49:24+01:00
2019-11-28T12:49:24+01:00[Europe/Amsterdam]
2019-11-28T11:49:24Z[UTC]
2019-11-28T20:49:24+09:00[Pacific/Palau]

编辑

The reason for the DateTimeParseException mentioned in your comment is the date-time String, because it doesn't have a zone or an offset, which makes it unparseable by the default DateTimeFormatter used in OffsetDateTime.parse(String datetime).

如果您有一个包含日期和时间信息但没有区域或偏移量的 String,您可以先将其解析为 LocalDateTime 并创建一个 ZonedDateTime 来自:

public static void main(String[] args) {
    // date time String without zone or offset information
    String dateTimeString = "2019-11-30T19:35:06";
    // create a LocalDateTime from the String
    LocalDateTime ldt = LocalDateTime.parse(dateTimeString);
    // then create a ZonedDateTime from the LocalDateTime adding a zone
    ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault()); // system default here
    // and print it
    System.out.println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}

关于java - java中的几个时区没有设置偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954574/

相关文章:

java - 为什么我的 TreeSet 不添加第一个元素之外的任何内容?

c# 重复事件(比如日历)

angularjs - Angular ui-calendar拖放对象

java - 为什么ZonedDateTime和Calendar在2050年的小时数上不一致?

java - Web View 不显示 HTML

java - 方法住在哪里?栈还是堆?

java - 调用方法中的数组被调用方法数组覆盖,没有任何返回语句

jakarta-ee - JPA 不能用于在 HttpSession 中管理的 CDI 范围?

java - Netbeans 找不到我的 Restful Web 服务

java - 在 DNS 重定向错误的情况下,发送的正确 HTTP 响应是什么?