gpt4 book ai didi

java - 简单 XML 是否支持 xsd :dateTime date format out of box?

转载 作者:行者123 更新时间:2023-11-29 05:24:10 25 4
gpt4 key购买 nike

<分区>

除了教程中提到的 2007-01-03 18:05:11.234 GMT 格式之外,Simple XML 是否支持任何日期格式?如果不是,我应该实现自定义 Transformer,还是有更简单的解决方案?

我为 xsd:dateTime 解析创建了一个测试用例,但目前失败了:

import org.junit.Test;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;

public class XsdDateTimeSimpleXmlTest {

String DUMMY_XML = "<dummy><date>2011-12-01T11:46:52</date></dummy>";

@Test
public void testDateTimeDeserialization() throws Exception {
Dummy expected = new Dummy(expectedDate());
Dummy actual = deserialize(DUMMY_XML);
assertEquals(expected, actual);
}

Date expectedDate() {
Calendar c = Calendar.getInstance();
c.set(2011, 12-1, 1, 11, 46, 52);
c.set(Calendar.MILLISECOND, 0);
c.setTimeZone(TimeZone.getTimeZone("UTC"));
return c.getTime();
}

Dummy deserialize(String xml) throws Exception {
Deserializer<Dummy> des = new Deserializer<Dummy>(Dummy.class);
return des.read(xml);
}

@Root
static class Dummy {

@Element
Date date;

Dummy() { }

Dummy(Date date) {
this.date = date;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dummy dummy = (Dummy) o;
if (!date.equals(dummy.date)) return false;
return true;
}

@Override
public int hashCode() {
return date.hashCode();
}

@Override
public String toString() {
return "Dummy{" +
"date=" + date +
'}';
}
}

}

我得到 java.text.ParseException: Unparseable date: "2011-12-01T11:46:52"

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