2012-11-28 18 views
7

मैं एक वेब सेवा के लिए क्लाइंट विकसित कर रहा हूं जो (.wsdl) अनुबंध का खुलासा करता है, जिसके लिए अनुरोध पैरामीटर पर 1 के लिए yyyy-MM-dd प्रारूप की आवश्यकता होती है, हालांकि ऑटो जेनरेट किया गया है .wsdl पर आधारित POJOS दिनांक 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 से दिनांक लिए जा रहा है/कैलेंडर अतिरिक्त समय/अवांछित डेटा कहते हैं कैलेंडर को कॉल करते समय मेरे yyyy-MM-dd को .getTime();

एक विशेष कोड खंड मैं XMLGregorianCalendar से जाने की जरूरत में अब तक का

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 नवम्बर 2012 18: 10: 39.743 डीबग com.tms.owners.integration। nsh.mapping.ServiceHistoryMapper - ServiceHistoryMapper, processRepairOrders(), repairOrderType.getCloseDate() से पहले: 2012-04-30

27-Nov-2012 18: 10: 51.413 डीबग com.tms.owners.integration.nsh .mapp ing.ServiceHistoryMapper - ServiceHistoryMapper, processRepairOrders(), repairOrderType.getCloseDate() के बाद: बुध मई 30 18:00:00 PDT 2012

आप ऊपर देख सकते हैं इससे पहले कि तिथि से पहले: 2012-04-30 और बाद दिनांक मई 30 18:00:00 पीडीटी 2012 अवांछित घंटों के साथ "18:00:00 पीडीटी" है।


नीचे अपने वास्तविक अनुरोध एक्सएमएल सेवा को भेजा जाता: 00 कि अतिरिक्त "-07:

<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 "डेटा जोड़ा गया है मैं बस 2012-05-30 चाहता हूँ।

धन्यवाद

उत्तर

5

एक्सएमएल के संदर्भ में XMLGregorianCalendar डेटाटाइप्स जो एक विधि पूर्णांक साल newXMLGregorianCalendarDate (कहा जाता है लगता है javax.xml.datatype.DatatypeFactory में कारखाने के तरीकों, पूर्णांक महीने, पूर्णांक दिन, पूर्णांक समय क्षेत्र के माध्यम से बनाई गई है);

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

यह कोड गलत है। आप केवल वर्ष निर्धारित कर रहे हैं –

2

तुम भी एक निम्न तरीके से जो अधिक पठनीय है में यह लिख सकते हैं:

GregorianCalendar gc = new GregorianCalendar(); 
gc.setTimeInMillis(c.getTimeInMillis()); 

XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); 
calendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED); 
calendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED); 
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; 
    } 
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); 
संबंधित मुद्दे