Java解析XML日期-排除时间
这是我第一次在 Java 中深入研究 XML。该代码使用JAXB生成类然后解析。我有一个带有日期的 XML...
JAXB 根据我的 XML 生成了一个类。它为该字段生成了以下内容:
@XmlElement(name = "CoverStartDate", required = true)
protected XMLGregorianCalendar coverStartDate;
在我的逻辑中,我有以下内容
xxxx.setCoverStartDate(xmlGregorianCalendar(theDate)
有一个方法 xmlGregorianCalendar 看起来像这样:
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
我生成的返回 XML 具有指定的日期和时间。我只想要日期(年月日)。
有什么建议吗?
谢谢
This is the first time I am working this intensly with XML in Java. The code uses JAXB to generate classes and then parse. I have an XML with a date...
A class was generated by JAXB from my XML. It generated the following for the field:
@XmlElement(name = "CoverStartDate", required = true)
protected XMLGregorianCalendar coverStartDate;
In my logic I have the following
xxxx.setCoverStartDate(xmlGregorianCalendar(theDate)
There is a method xmlGregorianCalendar which looks something like this:
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
My return XML that is generated, had the date with a time specified. I only want the date (year-month-day).
Any suggestions?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
DatatypeFactory.newXMLGregorianCalendarDate(...)
而不是简单地使用任何DatatypeFactory.newXMLGregorianCalendar(...)
方法。我不知道代码片段中的
theDate
是什么,但是如果您正在使用Date
对象,您可以使用以下内容。(请注意,
calendar
Calendar.ZONE_OFFSET
以毫秒为单位,而newXMLGregorianCalendarDate(...)
方法期望时区值以分钟为单位,因此需要 )(另请注意,Calendar 的月份索引是从 0 开始的,而 XMLGregorianCalendar 的月份索引是从 0 开始的 基于 1。)
如果这不起作用,那么您用于生成 JAXB 类的 XML 模式可能是错误的:可能它没有指定
xs:date
XML 模式类型的用法(可能它使用xs:dateTime
代替)。只有最后一个建议:手动创建 JAXB 类。然后您可以指定注释,例如
@XmlSchemaType
在你的类的字段上给你更多的控制权。Use
DatatypeFactory.newXMLGregorianCalendarDate(...)
instead of simply using any of theDatatypeFactory.newXMLGregorianCalendar(...)
methods.I don't know what is
theDate
in your code snippet, however if you're working withDate
objects you can use the following.(Note that
calendar's
Calendar.ZONE_OFFSET
is in milliseconds and thenewXMLGregorianCalendarDate(...)
method expects the timezone value in minutes, thus it needs to be converted.)(Also note that
Calendar's
month index is 0-based, whileXMLGregorianCalendar's
month is 1-based.)If this isn't working then the XML schema you've used to generate your JAXB classes is probably erroneous: maybe it does not specify the usage of the
xs:date
XML schema type (probably it usesxs:dateTime
instead).Only one last advice: create your JAXB classes by hand. Then you can specify annotations like
@XmlSchemaType
on your classes' fields giving you much more control.您需要使用 DateFormatter 将其格式化为仅 DATE 部分,忽略时间。
类似于:
(new SimpleDateFormat("yyyy MMMM dd")).format(theDate));
You need to use a DateFormatter that formats it into only the DATE part, ignoring the time.
Something like:
(new SimpleDateFormat("yyyy MMMM dd")).format(theDate));
您可以利用 JAXB 外部绑定文件,以便在对象模型中生成
GregorianCalendar
。这将消除您进行转换的需要。可以在此处找到执行类似操作的示例:javax.xml.bing.DatatypeConverter
类可用于创建必要的XmlAdapter
。You could leverage a JAXB external binding file to so that a
GregorianCalendar
is generated into your object model. This will eliminate the need for you to do the conversion. An example of doing something similar can be found here:The
javax.xml.bing.DatatypeConverter
class can be useful in creating the necessaryXmlAdapter
.这对我有用:
This works for me: