【问题标题】:set the timezone for the XMLGregorianCalendar设置 XMLGregorianCalendar 的时区
【发布时间】:2020-05-28 12:38:47
【问题描述】:

我有一个 XMLGregorianCalendar 实例,我需要将时区设置为“America/New_York”。该类具有设置时区的方法。由于格林威治标准时间比东部时间早 4 小时,它是否设置为“美国/纽约”时区?

gregorianCalendar.setTimezone(-4);

【问题讨论】:

  • 根据documentation,偏移量以分钟为单位。
  • 好的,但是如何设置时区 America/New_York?
  • 阅读此内容 - stackoverflow.com/questions/12137428/… 并尽可能避免使用旧版时间函数。
  • @OldProgrammer 是的,我将偏移量设置为分钟,现在可以使用了。

标签: java


【解决方案1】:

我最近使用了这个解决方案,对我来说效果很好。

Date date = format.parse(beginTime);

        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);

        XMLGregorianCalendar xmlGregCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);

        TimeZone timeZone = TimeZone.getTimeZone("America/New_York");
        int minutes = timeZone.getOffset(cal.getTimeInMillis()) / 1000 / 60;
        xmlGregCal.setTimezone(minutes);

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2012-09-11
    • 2018-05-13
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2019-08-22
    相关资源
    最近更新 更多