Java 使用偏移量将LocalDateTime格式化为XMLGregorianCalender

Java 使用偏移量将LocalDateTime格式化为XMLGregorianCalender,java,xml,datetime-format,xmlgregoriancalendar,Java,Xml,Datetime Format,Xmlgregoriancalendar,我有一个LocalDateTime的实例 我需要在这里使用JAXB将其映射到XMLGregorianCalendar,最后是XML,我希望XML文档中的时间如下所示: 2020-03-04T19:45:00.000+1:00 1小时是UTC的偏移量 我尝试使用DateTimeFormatter将LocalDateTime转换为字符串,然后将其映射到XMLGregorianCalendar 我现在有两个问题: 我在DateTimeFormatter中找不到任何格式化程序 哪种格式的时间偏移为UTC

我有一个LocalDateTime的实例

我需要在这里使用JAXB将其映射到XMLGregorianCalendar,最后是XML,我希望XML文档中的时间如下所示: 2020-03-04T19:45:00.000+1:00 1小时是UTC的偏移量

我尝试使用DateTimeFormatter将LocalDateTime转换为字符串,然后将其映射到XMLGregorianCalendar

我现在有两个问题:

我在DateTimeFormatter中找不到任何格式化程序 哪种格式的时间偏移为UTC?是这样的吗 存在还是我需要定义我的格式化程序模式

其次,如果我能够以字符串格式格式化LocalDateTime,那么 需要,如果我只是从创建一个XMLGregorianCalendar就足够了吗 字符串表示法


如果要从JVM的默认时区派生时区偏移量,请按如下方式编码:

LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault()); // <== default
OffsetDateTime offsetDateTime = zonedDateTime.toOffsetDateTime();
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
        .newXMLGregorianCalendar(offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

System.out.println(localDateTime);        // 2020-03-04T15:58:09.604171800
System.out.println(zonedDateTime);        // 2020-03-04T15:58:09.604171800-05:00[America/New_York]
System.out.println(offsetDateTime);       // 2020-03-04T15:58:09.604171800-05:00
System.out.println(xmlGregorianCalendar); // 2020-03-04T15:58:09.604171800-05:00
LocalDateTime localDateTime = LocalDateTime.now();
OffsetDateTime offsetDateTime = localDateTime.atOffset(ZoneOffset.ofHours(1)); // <== hardcoded
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
        .newXMLGregorianCalendar(offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

System.out.println(localDateTime);        // 2020-03-04T16:00:04.437550500
System.out.println(offsetDateTime);       // 2020-03-04T16:00:04.437550500+01:00
System.out.println(xmlGregorianCalendar); // 2020-03-04T16:00:04.437550500+01:00
LocalDateTime localDateTime = LocalDateTime.now();
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
        .newXMLGregorianCalendar(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
xmlGregorianCalendar.setTimezone(60); // <== hardcoded

System.out.println(localDateTime);        // 2020-03-04T16:03:09.032191
System.out.println(xmlGregorianCalendar); // 2020-03-04T16:03:09.032191+01:00
或者像这样:

LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault()); // <== default
OffsetDateTime offsetDateTime = zonedDateTime.toOffsetDateTime();
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
        .newXMLGregorianCalendar(offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

System.out.println(localDateTime);        // 2020-03-04T15:58:09.604171800
System.out.println(zonedDateTime);        // 2020-03-04T15:58:09.604171800-05:00[America/New_York]
System.out.println(offsetDateTime);       // 2020-03-04T15:58:09.604171800-05:00
System.out.println(xmlGregorianCalendar); // 2020-03-04T15:58:09.604171800-05:00
LocalDateTime localDateTime = LocalDateTime.now();
OffsetDateTime offsetDateTime = localDateTime.atOffset(ZoneOffset.ofHours(1)); // <== hardcoded
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
        .newXMLGregorianCalendar(offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

System.out.println(localDateTime);        // 2020-03-04T16:00:04.437550500
System.out.println(offsetDateTime);       // 2020-03-04T16:00:04.437550500+01:00
System.out.println(xmlGregorianCalendar); // 2020-03-04T16:00:04.437550500+01:00
LocalDateTime localDateTime = LocalDateTime.now();
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
        .newXMLGregorianCalendar(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
xmlGregorianCalendar.setTimezone(60); // <== hardcoded

System.out.println(localDateTime);        // 2020-03-04T16:03:09.032191
System.out.println(xmlGregorianCalendar); // 2020-03-04T16:03:09.032191+01:00

为什么偏移量应为+1:00?这是JVM的默认时区吗?类似但不相同:。我敢打赌还有很多。它可以是任何东西,取决于服务器和UTC的时区。这似乎是可行的。在某些情况下,我创建LocalDateTime对象时没有秒,或者将其设置为零秒,在这种情况下,xmlgregoriancalendar抛出异常。复制异常的示例代码:LocalDateTime paymentStatusTime=LocalDateTime.2020,1,1,9,30,0;ZonedDateTime gmtTime=ZonedDateTime.ofCalDateTime,ZoneId.ofSet;OffsetDateTime OffsetDateTime=gmtTime.toOffsetDateTime;System.out.PrintLNFOSET时间为+offsetDateTime;返回DatatypeFactory.newInstance.newXMLGregorianCalendaroffsetDateTime.toString@Nitesh将offsetDateTime.toString替换为offsetDateTime.formatDateTimeFormatter.ISO_OFFSET_DATE_Time谢谢我自己也尝试过同样的方法,事实上它确实起到了作用job@Nitesh这不是奇怪的行为,Z的使用对于XML模式数据类型有很好的定义,因此任何XML解析器都应该支持它,这是XMLGregorianCalendar所遵循的规范。-在中也指定了Z,这是XML架构定义格式的基础。-虽然dateTime的词汇表示法允许+00:00,但规范表示法指定必须将Z用于零偏移。@Nitesh如果有人不支持Z时区指定,那么它们就没有遵循XML模式标准/ISO-8601规范。他们真可耻-为什么不使用不使用Z作为偏移量的a来代替custo类型呢?例如,如果您只需要毫秒精度,模式uuu-MM-dd'HH:MM:ss.SSSxxx就可以了。