Offset is not set for few time zone in java

爷,独闯天下 提交于 2019-12-24 06:33:11

问题


I would want to take the time and time zone from user and create an entry. Used below calendar API to do this, it is working for few time zone and not working few time zones

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

Working timezone(at the end +xx:xx)

  • Pacific/Palau 2019-11-27T20:51:09.000+09:00
  • IST - 2019-11-20T22:00:00.000+05:30
  • Europe/Amsterdam - 2019-11-28T12:49:24.000+01:00
  • America/Los_Angeles - 2019-11-20T21:32:49.000-08:00

Not working time zone:-

  • Africa/Dakar - 2019-11-21T05:30:45.000Z
  • London(Europe/London) - 2019-11-21T12:08:42.000Z

For the above London and Africa/Dakar time zones do not have any indicator to distinguish the time zone, it simply specify “.000Z” at the end. Is there any attribute that we need to set in order to get full time zone? what does that .000z means?


回答1:


If you want to be able to write code that reflects the difference between offsets and time zones, leave java.util and switch to java.time (for Java 8+ and with a support library for Java 6 and 7).

Then you can do things like these:

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));
}

which outputs this

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]

EDIT

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).

If you have a String with date and time information but without a zone or an offset, you can parse it to a LocalDateTime first and create a ZonedDateTime from that:

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));
}



回答2:


You misunderstood. Z means exactly the same as +00:00 and is the conventional and recommended way to write it in the ISO 8601 format that you are producing. So for all of your time zones you are getting the correct UTC offset (except possibly IST; that may stand for Irish summer time or Israel standard time, in which case your offset of +05:30 is wrong; don't rely on ambiguous three letter tome zone abbreviations).

The Calendar and TimeZone classes are both poorly designed and long outdated. I recommend that instead of using those you use java.time, the modern Java date and time API. You need ZoneId and ZonedDateTime. See the answer by deHaar.



来源:https://stackoverflow.com/questions/58954574/offset-is-not-set-for-few-time-zone-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!