简体   繁体   中英

How to convert Date String to Calendar Date using Groovy in MarkupBuilder?

I am trying to convert a Date (String) like this 2015-03-26 15:26:38 to a Calendar Date like this: 2015-03-26T15:26:38.000Z to Send to a SOAP webservice.

I am in Mule ESB and using the Groovy Markup builder to convert JSON to XML and the date format is all I am missing. I tried with SimpleDateFormat but to no avail.

Here's my code :

def entityNs1 = [
    'xmlns:ns1': "http://schemas.datacontract.org/2004/07/PivotalService.Entities"
]

def entityNs0 = [
    'xmlns:ns0': "http://tempuri.org/"
]

def xml = new StringWriter().with { w -> new groovy.xml.MarkupBuilder(w).with {
        "ns0:SaveOrder"(entityNs0) {
            "ns0:order"() { 
                 "ns1:ContactPrestashopId"(entityNs1,payload.order.ContactPrestashopId)
                 "ns1:Discount"(entityNs1,payload.order.Discount)
                 "ns1:OrderDate"(entityNs1,payload.order.OrderDate)
                 "ns1:OrderNumber"(entityNs1,payload.order.OrderNumber)
                 "ns1:Total"(entityNs1,payload.order.Total)
                 "ns1:NumberOfChild"(entityNs1,payload.order.NumberOfChild)
                 "ns1:PaymentMethod"(entityNs1,payload.order.PaymentMethod)
                 "ns1:SpouseName"(entityNs1,payload.order.SpouseName)
                 "ns1:Products"(entityNs1) {
                    payload.order.Products.each { p -> "ns1:Product"() {
                             "ns1:Code"(p.Product.Code)
                             "ns1:Quantity"(p.Product.Quantity)
                             "ns1:UnitPrice"(p.Product.UnitPrice)
                        }
                    }
                }
            }
        }
    }
    w.toString()
}

Here's my XML result :

<ns0:SaveOrder xmlns:ns0='http://tempuri.org/'>
  <ns0:order>
    <ns1:ContactPrestashopId xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>112</ns1:ContactPrestashopId>
    <ns1:Discount xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>0.000000</ns1:Discount>
    <ns1:OrderDate xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>2015-03-26 15:26:38</ns1:OrderDate>
    <ns1:OrderNumber xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>VBOKLZZZF</ns1:OrderNumber>
    <ns1:Total xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>43.810000</ns1:Total>
    <ns1:NumberOfChild xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>2</ns1:NumberOfChild>
    <ns1:PaymentMethod xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>1</ns1:PaymentMethod>
    <ns1:SpouseName xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>Name name</ns1:SpouseName>
    <ns1:Products xmlns:ns1='http://schemas.datacontract.org/2004/07/PivotalService.Entities'>
      <ns1:Product>
        <ns1:Code>AB20</ns1:Code>
        <ns1:Quantity>1</ns1:Quantity>
        <ns1:UnitPrice>1</ns1:UnitPrice>
      </ns1:Product>
      <ns1:Product>
        <ns1:Code>AB20</ns1:Code>
        <ns1:Quantity>1</ns1:Quantity>
        <ns1:UnitPrice>1</ns1:UnitPrice>
      </ns1:Product>
    </ns1:Products>
  </ns0:order>
</ns0:SaveOrder>

In my previous connectore (Datamapper) I could just do this and it would work :

str2calendar(input.OrderDate, "yyyy-MM-dd' 'HH:mm:ss");

Just use Date.parse().format() as below:

assert Date.parse('yyyy-MM-dd HH:mm:ss', '2015-03-26 15:26:38')
           .format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") == '2015-03-26T15:26:38.000Z'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM