简体   繁体   English

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

[英]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. 我试图将这样的日期(字符串)2015-03-26 15:26:38转换为日历日期,例如:2015-03-26T15:26:38.000Z以发送到SOAP Web服务。

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. 我在Mule ESB中,使用Groovy标记构建器将JSON转换为XML,而日期格式是我所缺少的。 I tried with SimpleDateFormat but to no avail. 我尝试使用SimpleDateFormat,但无济于事。

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 : 这是我的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>

In my previous connectore (Datamapper) I could just do this and it would work : 在我以前的Connectore(Datamapper)中,我可以做到这一点,并且可以正常工作:

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

Just use Date.parse().format() as below: 只需使用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.

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