XMLGregorianCalendar来自String错误结果

时间:2014-12-09 19:19:42

标签: java xml date jaxb

我收到一个字符串并尝试将其转换为XMLGregorianCalendar,但我得到了错误的结果。

当前代码:

Flight flight = new flight();

XMLGregorianCalendar result = null;
Date date = null;
SimpleDateFormat simpleDateFormat;
GregorianCalendar gregorianCalendar;

try {   
    simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");        
    date = simpleDateFormat.parse(flightDate);
    gregorianCalendar = (GregorianCalendar)GregorianCalendar.getInstance();
    gregorianCalendar.setTime(date);
    result = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
} catch (ParseException ex) {
    Logger.getLogger(FlightBookings.class.getName()).log(Level.SEVERE, null, ex);
} catch (DatatypeConfigurationException ex) {
    Logger.getLogger(FlightBookings.class.getName()).log(Level.SEVERE, null, ex);
}

flight.setDate(result);

所以我的飞行物体有这个属性:

protected XMLGregorianCalendar date;

并设置此功能:

public void setDate(XMLGregorianCalendar value) {
    this.date = value;
}

然后我使用以下方法打印flight个对象日期:

System.out.println(flight.getDate());

我明白了:

2014-06-04T09:15:00.000+01:00

但是当我使用jaxbm将其编组为XML文档时,XML中显示的值为:

2014-06-04+01:00

我想保存到XML字段的结果应如下所示:

04/06/2014 09:15

任何想法出了什么问题? 感谢。

更新:

飞行课程:

public static class Flight {

    @XmlElement(name = "ID")
    protected int id;
    @XmlElement(name = "Origin_City", required = true)
    protected String originCity;
    @XmlElement(name = "Destination_City", required = true)
    protected String destinationCity;
    @XmlElement(name = "Airline", required = true)
    protected String airline;
    @XmlElement(name = "Available_Seats")
    protected int availableSeats;
    @XmlElement(name = "Number_Of_Connections")
    protected int numberOfConnections;
    @XmlElement(name = "Date", required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar date;
    @XmlElement(name = "Fare", required = true)
    protected FareType fare;

还包括所有get和set函数。

1 个答案:

答案 0 :(得分:0)

非常简单的错误。我在XML Schema中将类型设置为date,但应该是dateTime。这就是没有保存时​​间的原因。