gpt4 book ai didi

java - 如何将 java.util.sql 存储到 XMLGregorianCalendar

转载 作者:行者123 更新时间:2023-12-01 14:04:50 24 4
gpt4 key购买 nike

我正在尝试更改 XMLGregorianCalendar 日期的格式。我的架构文件(.xsd)中的代码是这样的:

    <xs:element name="LatestSaleDate">
<xs:annotation>
<xs:documentation>Latest sale date on the property (format MM/DD/YYYY)</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:date"/>
</xs:simpleType>
</xs:element>

我通过 XJC 命令创建了 Java 类

我有 setter 和 getter 将latestsaledate 设置为:

    @XmlElement(name = "LatestSaleDate")
protected XMLGregorianCalendar latestSaleDate;

但是当我尝试从数据库获取日期并分配给 XMLGregorianCalendar 对象时,它给了我 IllegalArgumentException

这里的任何人都可以帮助我如何解决它并格式化它,以便它只以这种格式 dd/MM/yyyy 写入 xml 文件

最佳答案

Can anybody here help me how I can solve it and also format it so it only write to xml file in this format dd/MM/yyyy

您不能也不应该 - 至少在不更改架构的情况下不能这样做。您的架构明确指定它是 xs:date - 以及 specified format of xs:date基本上是 ISO-8601(yyyy-MM-dd,带有可选的 UTC 偏移量),不是 dd/MM/yyyy。

我强烈建议您以标准化表示形式存储数据 - 不要忘记用户永远不必看到它;您可以根据用户的文化来解析/格式化日期,然后以 ISO-8601 格式存储。区分存储/通信格式和表示格式非常重要。

关于java - 如何将 java.util.sql 存储到 XMLGregorianCalendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18986254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com