我正在开发一个客户端到一个公开(.wsdl)合同的Web服务,它需要在请求参数上使用yyyy-MM-dd格式为1,但是基于.wsdl的自动生成的POJOS创建日期属性为输入XMLGregorianCalendar。
我的问题不是转换到XMLGregorianCalendar或从XMLGregorianCalendar转换,请参阅下面的实用程序:
public static XMLGregorianCalendar toXMLGregorianCalendar(Calendar c){
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(c.getTimeInMillis());
XMLGregorianCalendar xc= null;
try {
xc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xc;
}
我的问题是从XMLGregorianCalendar转到Date / Calendar,在调用calendar.getTime()时,会向yyyy-MM-dd添加额外的时间/不需要的数据;
在特定代码段中,我需要从XMLGregorianCalendar转到Date
if (repairOrderType.getCloseDate() != null) {
LOG.debug("ServiceHistoryMapper, processRepairOrders() , repairOrderType.getCloseDate() BEFORE:"
+ repairOrderType.getCloseDate());
String date = repairOrderType.getCloseDate().getYear() + "-"
+ repairOrderType.getCloseDate().getMonth() + "-"
+ repairOrderType.getCloseDate().getDay();
//Approach #1, trying to remove hour,minute,sec values by calendar.clear() method , not successful
Calendar calendar = Calendar.getInstance();
calendar.set(repairOrderType.getCloseDate().getYear(),
repairOrderType.getCloseDate().getMonth(),
repairOrderType.getCloseDate().getDay());
calendar.clear(Calendar.HOUR);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
/*Approach#2 , trying to remove hour,minute,sec values using SimpleDateFormat ,
* also not successful. SimpleDateFormat or DateFormat are use to format String output NOT remove internal data
*
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = formatter.getCalendar();
calendar.set(repairOrderType.getCloseDate().getYear(),
repairOrderType.getCloseDate().getMonth(),
repairOrderType.getCloseDate().getDay());
*/
LOG.debug("ServiceHistoryMapper, processRepairOrders() , repairOrderType.getCloseDate() AFTER:"
+ calendar.getTime());
repairOrder.setCloseDate(calendar.getTime());
}
输出:
27-Nov-2012 18:10:39.743 DEBUG com.tms.owners.integration.nsh.mapping.ServiceHistoryMapper - ServiceHistoryMapper,processRepairOrders(),repairOrderType.getCloseDate() BEFORE:2012-04-30 < /强>
27-Nov-2012 18:10:51.413 DEBUG com.tms.owners.integration.nsh.mapping.ServiceHistoryMapper - ServiceHistoryMapper,processRepairOrders(),repairOrderType.getCloseDate()AFTER:Wed 5月30日18:00 :00 PDT 2012
正如您在上面看到的那样,日期之前是2012-04-30,而日期是 5月30日18:00:00 PDT 2012 ,不需要的时间“18:00:00 PDT”。
以下是我发送给服务的XML实际请求:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns4:VehicleServiceHistoryDetails
xmlns="urn:tms.toyota.com/Components" xmlns:ns2="urn://esb.ari.xxxxxx.com/2008/12/10/schemas/common/Customer"
xmlns:ns3="urn:incentives.ari.xxxxxx.com/StandardHeader"
xmlns:ns4="urn://esb.ari.xxxxxx.com/2008/12/10/schemas/History"
xmlns:ns5="http://ice.ari.xxxxxx.com/EMF" xmlns:ns6="urn:ari.xxxxxx.com/rtmheader">
<ns5:ApplicationArea>
<ns5:CreationDateTime>2012-11-27T18:11:23.071-08:00
</ns5:CreationDateTime>
<ns5:Sender />
<ns5:UserArea />
</ns5:ApplicationArea>
<ns4:VehicleServiceHistoryDataArea>
<ns4:VehicleServiceHistoryHeader>
<ns3:TimeStamp>2012-11-27T18:11:23.071-08:00</ns3:TimeStamp>
<ns3:SourceSystem>TOO</ns3:SourceSystem>
<ns4:SourceKey>TOY1TWXE</ns4:SourceKey>
</ns4:VehicleServiceHistoryHeader>
<ns4:VehicleServiceHistory>
<ns4:VIN>XXXXXXXXXXXXXXXX</ns4:VIN>
<ns4:RepairOrder>
<ns2:RepairOrderDealer>
<DealerNumber>29059</DealerNumber>
</ns2:RepairOrderDealer>
<ns2:RepairOrderNumber>0088745</ns2:RepairOrderNumber>
<ns2:CloseDate>2012-05-30-07:00</ns2:CloseDate>
</ns4:RepairOrder>
</ns4:VehicleServiceHistory>
</ns4:VehicleServiceHistoryDataArea>
</ns4:VehicleServiceHistoryDetails>
</S:Body>
</S:Envelope>
你可以在2012-05-30-07:00的xml请求中看到我只想要2012-05-30添加额外的“-07:00”数据。
谢谢
答案 0 :(得分:5)
在XML数据类型的上下文中,XMLGregorianCalendar是通过javax.xml.datatype.DatatypeFactory中的工厂方法创建的,它似乎有一个名为newXMLGregorianCalendarDate的方法(int year,int month,int day,int timezone);
所以我创建了一个实用工具方法:
public static XMLGregorianCalendar toXMLGregorianCalendarDateOnly(Calendar c){
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(c.getTimeInMillis());
XMLGregorianCalendar xc= null;
try {
xc = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(gc.get(Calendar.YEAR),Calendar.MONTH,Calendar.DAY_OF_MONTH,DatatypeConstants.FIELD_UNDEFINED);
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xc;
}
现在问题已解决,我们正在获得所需的yyyy-MM-ddd。
答案 1 :(得分:3)
您也可以通过以下方式编写更易读的内容:
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(c.getTimeInMillis());
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
calendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
calendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
答案 2 :(得分:0)
我使用以下代码解决了同样的问题。
public static XMLGregorianCalendar toXMLGregorianCalendarWithoutTimeStamp(String date) {
Date mDate = null;
GregorianCalendar cal = new GregorianCalendar();
XMLGregorianCalendar xmlGregorianCalendar;
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
try {
mDate = df.parse(date);
cal.setTime(mDate);
xmlGregorianCalendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendarDate(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH) + 1,
cal.get(Calendar.DAY_OF_MONTH),
DatatypeConstants.FIELD_UNDEFINED);
return xmlGregorianCalendar;
} catch (DatatypeConfigurationException e) {
LOGGER.error("Error in getCustomerCDRResponse Date Formate Type Configuartion :: " + e);
} catch (ParseException e) {
LOGGER.error("Error in getCustomerCDRResponse Date Parsing :: " + e);
}
return null;
}
答案 3 :(得分:0)
试试这个。
Date dob=null;
DateFormat df=new SimpleDateFormat("dd/MM/yyyy");
dob=df.parse( "10/02/2014 11:15:00" );
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(dob);
XMLGregorianCalendar xmlDate3 = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH),DatatypeConstants.FIELD_UNDEFINED);
System.out.println(xmlDate3);
答案 4 :(得分:0)
如果使用javax.xml.bind.Marshaller准备SOAP正文,则不需要的行为的根在您的xlmns:ns2中。字段ns2:CloseDate是类型(或其他一些类型,其中包括日期和时间):
{http://www.w3.org/2001/XMLSchema}dateTime
将其更改为(或其他不带小时和分钟的日期类型):
{http://www.w3.org/2001/XMLSchema}date
如果您不管理此xlmns,只需接受您的CloseDate必须定义时间。没有它,称为Web服务的时间声明将不正确(我不知道确切的值,但是我打赌0:00)