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

时间:2015-04-28 17:07:00

标签: java date groovy mule

我正在尝试将像这个2015-03-26 15:26:38这样的日期(String)转换为这样的日历日期:2015-03-26T15:26:38.000Z发送到SOAP Web服务。< / p>

我在Mule ESB中并使用Groovy Markup构建器将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");

1 个答案:

答案 0 :(得分:1)

只需使用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'