如何在 MarkupBuilder 中使用 Groovy 将日期字符串转换为日历日期?

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

我正在尝试将 2015-03-26 15:26:38 这样的日期(字符串)转换为这样的日历日期:2015-03-26T15:26:38.000Z 以发送到SOAP 网络服务。

我在 Mule ESB 中并使用 Groovy 标记生成器将 JSON 转换为 XML,我只缺少日期格式。我尝试使用 SimpleDateFormat 但无济于事。

这是我的代码:

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()
}

这是我的 XML 结果:

<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>

在我以前的连接器 (Datamapper) 中,我可以这样做并且它会起作用:

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

只需使用 Date.parse().format() 如下:

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'