我使用xmlText()方法获取XmlObject的Xml表示形式。 XmlDateTime对象在根据XML Schema: dateTime有效的字符串末尾带有时区偏移。有没有办法强制XmlObject转换为Zulu格式的XML?XmlBeans XmlDateTime格式不带时区信息
闻听此事:2002-10-10T12:00:00-05:00 ,需要这个:2002-10-10T17:00:00Z
我使用xmlText()方法获取XmlObject的Xml表示形式。 XmlDateTime对象在根据XML Schema: dateTime有效的字符串末尾带有时区偏移。有没有办法强制XmlObject转换为Zulu格式的XML?XmlBeans XmlDateTime格式不带时区信息
闻听此事:2002-10-10T12:00:00-05:00 ,需要这个:2002-10-10T17:00:00Z
我问的是XmlDateTime对象,因为的实例前段时间我遇到了类似的问题。从我所知道的情况来看,XmlDateTime被打印到xml的方式取决于内部表示的值,而内部表示又取决于调用提供该值的setter。问题出在setDate(...)方法。
XmlDateTime的默认实现保持日期时间价值的内部作为使用GDateBuilder建立了一个org.apache.xmlbeans.GDate。当你在XmlDateTime对象上设置日期时,它最终将值传递给GDateBuilder。 如果你看的setDate()方法的来源,javadoc中指出:
Sets the current time and date based on a java.util.Date instance.
The timezone offset used is based on the default TimeZone. (The default TimeZone is consulted to incorporate daylight savings offsets if applicable for the current date as well as the base timezone offset.)
If you wish to normalize the timezone, e.g., to UTC, follow this with a call to normalizeToTimeZone.
由于XmlDateTime对象具有setGDate(...)方法可以测试标准化的方法是这样的:
XmlDateTime xmlDateTime = XmlDateTime.Factory.newInstance();
xmlDateTime.setStringValue("2002-10-10T12:00:00-05:00");
System.out.println(xmlDateTime.xmlText());
GDateBuilder gdb = new GDateBuilder(xmlDateTime.getDateValue());
gdb.normalize();
xmlDateTime.setGDateValue(gdb.toGDate());
System.out.println(xmlDateTime.xmlText());
对于我这种印刷:
<xml-fragment>2002-10-10T12:00:00-05:00</xml-fragment>
<xml-fragment>2002-10-10T17:00:00Z</xml-fragment>
那是我能得到它在UTC打印的唯一途径。
我希望有更好的办法,虽然可惜我找不到它...
你是如何构建XmlDateTime对象实例?你是否沿着'XmlDateTime.Factory.newInstance()'和'setDate(...)'的方向使用了一些东西? – 2012-02-27 17:01:57
它以XML的格式进入,但后续系统无法处理它。所以我们需要转换为UTC格式。其实我只是发现这不再是一个问题,但我仍然对如何处理这种情况感兴趣。 – GrkEngineer 2012-02-27 17:52:26