Skip to Content
0
Jun 07, 2021 at 11:00 AM

Groovy Script to change timezone of XML element

236 Views Last edit Jun 07, 2021 at 11:01 AM 2 rev

Hi All,

I want to change change the time zone one of the element of XML file and then return message.

I am getting below error :-

def Message processData(Message message)
{
    def body = message.getBody(Reader);
    def xml = new XmlSlurper().parse(body);
    def  tz = TimeZone.getTimeZone("Asia/Kolkata");
    def ts = new Date();
    def user = xml.Resources.User.findAll{it.Separation_Date.text != null}
    println user.Separation_Date
    user.each{ Separation_Date ->

        Separation_Date.ts.format("HH:mm:ss", tz);

    }    
    message.setBody(xml)
    return message
}

Error :-

Console Output
Running...
2021-04-19T10:00:00.000Z
***
Error at line: 18
***
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChildren.format() is applicable for argument types: (String, sun.util.calendar.ZoneInfo) values: [HH:mm:ss, sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]]
Possible solutions: first(), sort(), parent(), toFloat(), toSet(), max()
    at com.groovyide.ExecutorService$1.run(ExecutorService.java:102)
Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChildren.format() is applicable for argument types: (String, sun.util.calendar.ZoneInfo) values: [HH:mm:ss, sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]]
Possible solutions: first(), sort(), parent(), toFloat(), toSet(), max()
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:49)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135)
    at groovyide_com$_processData_closure2.doCall(groovyide_com:18)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:263)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041)
    at groovy.lang.Closure.call(Closure.java:405)
    at groovy.lang.Closure.call(Closure.java:421)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2330)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2315)
    at org.codehaus.groovy.runtime.dgm$182.doMethodInvoke(Unknown Source)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1217)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041)
    at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:147)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:37)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
    at groovyide_com.processData(groovyide_com:16)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1217)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:823)
    at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44)
    at groovy.lang.Script.invokeMethod(Script.java:77)
    ... 1 more
Error:
TypeError: Cannot convert undefined or null to object

<br>

What is wrong with my code?

TIA,

Rashmi