Java解析XML日期-排除时间

发布于 2024-12-09 12:36:29 字数 662 浏览 0 评论 0原文

这是我第一次在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箜明 2024-12-16 12:36:29

使用 DatatypeFactory.newXMLGregorianCalendarDate(...) 而不是简单地使用任何DatatypeFactory.newXMLGregorianCalendar(...) 方法。

我不知道代码片段中的 theDate 是什么,但是如果您正在使用 Date 对象,您可以使用以下内容。

  public static XMLGregorianCalendar setCoverStartDate(Date date) throws DatatypeConfigurationException {
    Calendar calendar = Calendar.getInstance();
    date.setTime(date.getTime());
    return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
        calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH),
        getTimeZone(calendar));
  }

  public static int getTimeZone(Calendar calendar) {
    return (int) TimeUnit.MINUTES.convert(calendar.get(Calendar.ZONE_OFFSET), TimeUnit.MILLISECONDS);
  }

(请注意,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 the DatatypeFactory.newXMLGregorianCalendar(...) methods.

I don't know what is theDate in your code snippet, however if you're working with Date objects you can use the following.

  public static XMLGregorianCalendar setCoverStartDate(Date date) throws DatatypeConfigurationException {
    Calendar calendar = Calendar.getInstance();
    date.setTime(date.getTime());
    return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
        calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH),
        getTimeZone(calendar));
  }

  public static int getTimeZone(Calendar calendar) {
    return (int) TimeUnit.MINUTES.convert(calendar.get(Calendar.ZONE_OFFSET), TimeUnit.MILLISECONDS);
  }

(Note that calendar's Calendar.ZONE_OFFSET is in milliseconds and the newXMLGregorianCalendarDate(...) method expects the timezone value in minutes, thus it needs to be converted.)

(Also note that Calendar's month index is 0-based, while XMLGregorianCalendar'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 uses xs: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.

善良天后 2024-12-16 12:36:29

您需要使用 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));

旧情别恋 2024-12-16 12:36:29

您可以利用 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 necessary XmlAdapter.

眼眸印温柔 2024-12-16 12:36:29

这对我有用:

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

Date fechaBajaPrevista = Calendar.getInstance().getTime();

XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(
                        new SimpleDateFormat("yyyy-MM-dd").format(fechaBajaPrevista));

This works for me:

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

Date fechaBajaPrevista = Calendar.getInstance().getTime();

XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(
                        new SimpleDateFormat("yyyy-MM-dd").format(fechaBajaPrevista));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文