<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How to work with java.util.Date in IvyScript-Step without auto initialization</title><link>https://answers.axonivy.com/questions/52/how-to-work-with-java-util-date-in-ivyscript-step-without-auto-initialization</link><description>&lt;p&gt;In an IvyScriptStep variables are auto initialized as soon as they are accessed. The following code will log "bad":&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;java.util.Date date = null;
if ( date!=null) {
    ivy.log.info("bad");
} else {
    ivy.log.info("good");
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It looks like the object "date" will never be a java.util.Date, it is an IvyDate. There is also no chance to import java.util.Date because of a conflict.&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/52/how-to-work-with-java-util-date-in-ivyscript-step-without-auto-initialization" rel="self"></atom:link><language>en</language><lastBuildDate>Fri, 08 Nov 2013 15:43:26 -0500</lastBuildDate><item><title>Answer by Flavio Sadeghi</title><link>https://answers.axonivy.com/questions/52/how-to-work-with-java-util-date-in-ivyscript-step-without-auto-initialization/60</link><description>&lt;p&gt;&lt;a href="http://www.xpertivy.ch/releases/Xpert.ivy/5.0.latest/documents/DesignerGuideHtml/ivy.ivyscript.html"&gt;IvyScript&lt;/a&gt; auto initialise default types like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Strings with an empty String&lt;/li&gt;
&lt;li&gt;Numbers with zero&lt;/li&gt;
&lt;li&gt;java.util.Date with a default value of '0001-01-01 00:00:00'&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br&gt;
To handle this &lt;strong&gt;inside IvyScript&lt;/strong&gt; the &lt;strong&gt;&lt;code&gt;is initialized&lt;/code&gt; statement&lt;/strong&gt; is the recommended operator to check if a variable is null or was auto initialized. Example: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Date myDate = foo.bar.getMyDate();
if (myDate is initialized) {
    ivy.log.info("myDate is not the default value");    
} else {
    ivy.log.error("myDate is auto initialized");    
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
As long as we are working inside IvyScript anything works fine. When we want to use the variable as a parameter, e.g. to &lt;strong&gt;call a method in Java&lt;/strong&gt;, we have to pay attention to the auto initialization. Example: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// getMyDate() returns null
Date myDate = foo.bar.getMyDate();

// below line prints: 'Sat Jan 01 00:00:00 CET 1', 
// because is was auto initialized even getMyDate() returns null
MyJavaClass.printDate(myDate);

// below line prints: 'null', becaue we pass null if the variable was auto initialized
MyJavaClass.printDate(myDate is initialized ? myDate : null);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
If we &lt;strong&gt;navigating through beans&lt;/strong&gt; we could also use the &lt;strong&gt;&lt;code&gt;#&lt;/code&gt;-operator&lt;/strong&gt;. This operator prevents auto initialization. Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (in.foo.#adressBean == null)
{
   // yes, the field 'adressBean' is null and was not auto initialized
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
When we working with &lt;strong&gt;types which could not be initialized&lt;/strong&gt; (like interfaces), we have to use the &lt;strong&gt;&lt;code&gt;#&lt;/code&gt;-operator&lt;/strong&gt; too.  Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;IInterfaceX interfaceX = foo.bar.getInterfaceX();
if (#interfaceX != null)
{
   // call a method on the interface.
}

// below call will fail with an exception, because IvyScript could not auto initialized an interface
interfaceX.getString();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
See also the chapter &lt;a href="http://www.xpertivy.ch/releases/Xpert.ivy/5.0.latest/documents/DesignerGuideHtml/ivy.ivyscript.html#ivy.ivyscript.language.nullhandling"&gt;IvyScript &amp;amp; Null Handling&lt;/a&gt; for more details.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Flavio Sadeghi</dc:creator><pubDate>Fri, 08 Nov 2013 15:43:26 -0500</pubDate><guid>https://answers.axonivy.com/questions/52/how-to-work-with-java-util-date-in-ivyscript-step-without-auto-initialization/60</guid></item><item><title>Answer by Adrian Imfeld</title><link>https://answers.axonivy.com/questions/52/how-to-work-with-java-util-date-in-ivyscript-step-without-auto-initialization/53</link><description>&lt;p&gt;A simple solution is to transfer the code to a handler class and call the methode from the IvyScript-Step.&lt;/p&gt;
&lt;p&gt;If you still want the code in a ScriptStep, you could do the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import java.util.concurrent.atomic.AtomicReference;
AtomicReference date = new AtomicReference();
//date.set(new java.util.Date());
if ( date.get()!=null) {
  ivy.log.info("bad");
} else {
  ivy.log.info("good");
}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Adrian Imfeld</dc:creator><pubDate>Wed, 06 Nov 2013 14:45:48 -0500</pubDate><guid>https://answers.axonivy.com/questions/52/how-to-work-with-java-util-date-in-ivyscript-step-without-auto-initialization/53</guid></item></channel></rss>