XmlBeans XmlDateTime格式,没有时区信息 [英] XmlBeans XmlDateTime format without timezone info

查看:101
本文介绍了XmlBeans XmlDateTime格式,没有时区信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xmlText()方法获取XmlObject的Xml表示形式. XmlDateTime对象在字符串的末尾带有时区偏移,根据 XML架构:dateTime .有什么方法可以强制XmlObject以Zulu格式转换为xml?

I'm getting an Xml representation of an XmlObject using the xmlText() method. The XmlDateTime objects are coming out with timezone offsets at the end of the string which is valid according to XML Schema: dateTime. Is there any way to force the XmlObject to convert to xml with the Zulu formatting?

获取此信息:2002-10-10T12:00:00-05:00 而是需要这个:2002-10-10T17:00:00Z

Getting this: 2002-10-10T12:00:00-05:00 and need this instead: 2002-10-10T17:00:00Z

推荐答案

我问XmlDateTime对象的实例化是因为前一段时间我遇到了类似的问题.据我所知,将XmlDateTime打印到xml的方式取决于内部表示的值,而内部表示又取决于调用提供该值的setter.问题出在setDate(...)方法上.

I was asking about the instantiation of the XmlDateTime object because I ran into a similar issue a while ago. From what I could figure out, the way the XmlDateTime is printed to xml depends on the value of the internal representation, which in turn depended on the setter which was invoke to provide that value. The issue was with the setDate(...) method.

XmlDateTime的默认实现在内部将datetime的值保留为使用GDateBuilder构建的org.apache.xmlbeans.GDate.当您在XmlDateTime对象上设置日期时,它最终会将值传递到GDateBuilder上. 如果您查看setDate()方法的源代码,则Javadoc会指出:

The default implementation of XmlDateTime keeps the value of the datetime internally as an org.apache.xmlbeans.GDate which is built using a GDateBuilder. When you set the date on the XmlDateTime object it eventually passes the value onto a GDateBuilder. If you look at the source of the setDate() method, the javadoc states that:

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(...)方法,因此您可以像下面那样测试normalize方法:

Since the XmlDateTime object has a setGDate(...) method you can test the normalize method like this:

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上打印它的唯一方法.

That was the only way that I could get it to print in UTC.

我希望有更好的方法,尽管遗憾的是我找不到它...

I hope there is a better way, although sadly I couldn't find it...

这篇关于XmlBeans XmlDateTime格式,没有时区信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆