cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script to change timezone of XML element

rasjoshi
Active Contributor
0 Kudos
912

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

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member522497
Participant
0 Kudos

Hi Rashmi,

Please try the below snippet of code and it should work for fetching the time zone of Asia/Kolkata:

Hope this helps!

Please mark the response as correct if it resolves the issue.

Thanks,

Ameer

Former Member
0 Kudos

Hello Rashmi,

I tried to understand the requirement and gave my response in accordance, but it would help if you can provide some more inputs like what is the timezone that you are converting from, I didn't see that anywhere in the code.

The below line needs to be updated it seems! Also, don't forget to serialize and build your XML post manipulation.

user.each{ Separation_Date ->
        Separation_Date = ts.format("HH:mm:ss", tz);
    }    
    //println(XmlUtil.serialize(xml))