title: Java template function
body: <p>I have a function that sometimes has to return a <code>Date</code> other times a <code>DateTime</code> (Joda-Time).</p>
  <pre class="lang-java prettyprint-override"><code>static public &lt;T extends Object&gt; T convertTimeForServer(DateTime toSave) {
      DateTime temp = null;
      try {
          temp = toSave.withZone(DateTimeZone.forID(getServerTimeZone()));
      } catch (Exception e) {
      }
  
      T toReturn = null;
      if (toReturn.getClass().equals(temp)) {
          return (T) temp;//Return DATETIME
      } else {
          return (T) temp.toDate();//Return DATE
      }
  }
  </code></pre>
  <p>Is it the right approach?<br />
  How to use it?</p>
  <p>like this (timerHelper is the name of class):</p>
  <pre class="lang-java prettyprint-override"><code>DateTime t = timerHelper.&lt;DateTime&gt;convertTimeForServer(new DateTime());
  Date t2 = timerHelper.&lt;Date&gt;convertTimeForServer(new DateTime());
  </code></pre>
  <p>or</p>
  <pre><code>DateTime t = (DateTime)timerHelper.convertTimeForServer(new DateTime());
  Date t2 = (Date)timerHelper.convertTimeForServer(new DateTime());
  </code></pre>
  <p>And how to use this function instead?</p>
  <pre class="lang-java prettyprint-override"><code>static public &lt;T extends Object&gt; T current_Moment(){
      return convertTimeForServer(new DateTime());
  }
  </code></pre>