[英]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 Web服務。
我在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>
在我以前的Connectore(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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.