java 从 XMLGregorianCalendar 到日期/日历增加了额外的时间/不需要
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13596740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
from XMLGregorianCalendar to Date/Calendar adds extra time/unwanted
提问by cyber101
I'm developing a client to a web service which exposes (.wsdl) contract, which requires yyyy-MM-dd format for 1 on the request parameters , however auto generated POJOS based on the .wsdl create the date attribute as Type XMLGregorianCalendar.
我正在开发一个暴露 (.wsdl) 合同的 Web 服务的客户端,它需要 yyyy-MM-dd 格式为 1 的请求参数,但是基于 .wsdl 的自动生成的 POJOS 创建日期属性作为类型 XMLGregorianCalendar。
My issue is NOT converting to or from XMLGregorianCalendar see my utility below:
我的问题不是从 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;
}
My issue is going from XMLGregorianCalendar to Date/Calendar adds extra time/unwanted data to my yyyy-MM-dd when calling calendar.getTime();
我的问题是从 XMLGregorianCalendar 到 Date/Calendar 在调用 calendar.getTime() 时向我的 yyyy-MM-dd 添加了额外的时间/不需要的数据;
In a particular code segment I need to go from XMLGregorianCalendar to Date
在特定的代码段中,我需要从 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());
}
Output:
输出:
27-Nov-2012 18:10:39.743 DEBUG com.tms.owners.integration.nsh.mapping.ServiceHistoryMapper - ServiceHistoryMapper, processRepairOrders() , repairOrderType.getCloseDate() BEFORE:2012-04-30
2012 年 11 月 27 日 18:10:39.743 调试 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 May 30 18:00:00 PDT 2012
2012 年 11 月 27 日 18:10:51.413 调试 com.tms.owners.integration.nsh.mapping.ServiceHistoryMapper - ServiceHistoryMapper、processRepairOrders()、repairOrderType.getCloseDate() 之后:太平洋夏令时间 5 月 30 日星期三 18:00:002
As you can see above BEFORE date is BEFORE:2012-04-30 and AFTER date is May 30 18:00:00 PDT 2012with unwanted hours "18:00:00 PDT".
正如您在上面看到的,BEFORE 日期是 BEFORE:2012-04-30,AFTER 日期是2012 年 5 月 30 日 18:00:00 PDT,不需要时间“18:00:00 PDT”。
Below is my actual request XML sent to the service:
下面是我发送到服务的实际请求 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>
You can see in the request xml in the 2012-05-30-07:00 that extra "-07:00" data is added i just want 2012-05-30.
您可以在 2012-05-30-07:00 的请求 xml 中看到添加了额外的“-07:00”数据,我只想要 2012-05-30。
Thanks
谢谢
回答by cyber101
The in context of XML datatypes the XMLGregorianCalendar is created via factory methods in javax.xml.datatype.DatatypeFactory which seems to have a method called newXMLGregorianCalendarDate(int year, int month, int day, int timezone);
XMLGregorianCalendar 在 XML 数据类型的上下文中是通过 javax.xml.datatype.DatatypeFactory 中的工厂方法创建的,它似乎有一个名为 newXMLGregorianCalendarDate(int year, int month, int day, int timezone); 的方法。
So I created a utility method :
所以我创建了一个实用方法:
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;
}
The issue is resolved now, we are getting the desired yyyy-MM-ddd.
问题现已解决,我们得到了所需的 yyyy-MM-ddd。
回答by zygimantus
You can also write it in a following manner which is more readable:
您还可以按以下方式编写它,这样更具可读性:
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(c.getTimeInMillis());
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
calendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
calendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
回答by Rajesh D
I used the below code to resolve the same problem.
我使用下面的代码来解决同样的问题。
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;
}
回答by Viroo
try this.
试试这个。
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);
回答by Kuba Samczuk
If you are using javax.xml.bind.Marshaller to prepare SOAP body then the root of the unwanted bahavior is in yours xlmns:ns2. The field ns2:CloseDate is of type (or some other type wichich include date and time):
如果您使用 javax.xml.bind.Marshaller 来准备 SOAP 主体,那么不需要的行为的根源就在您的 xlmns:ns2 中。字段 ns2:CloseDate 是类型(或其他一些类型,其中包括日期和时间):
{http://www.w3.org/2001/XMLSchema}dateTime
change it to (or other date type without hour and minute):
将其更改为(或其他不含小时和分钟的日期类型):
{http://www.w3.org/2001/XMLSchema}date
If you don't manage this xlmns, just accept that yours CloseDate must have time defined. Without it called web service would have unapropiate time declaration (I don't know exactly value, but I'm bet on 0:00)
如果您不管理此 xlmns,只需接受您的 CloseDate 必须定义时间。没有它,称为 Web 服务的时间声明将不准确(我不知道确切的值,但我打赌 0:00)