title: Compare timestamp columns - Spark scala
body: <p>I am trying to compare 2 timestamp columns and return the value which is minimum value among the two and wondering is there  a better way than what I have. Note both columns might have values / one of them has value and other could be null. I know about When.otherwise.</p>
  <pre><code>import org.apache.spark.sql.functions._
  import java.sql.Timestamp
   val compareTime = udf((t1: Timestamp, t2: Timestamp) =&gt; {
      if(t1 != null &amp;&amp; t2 != null &amp;&amp; t1.before(t2) ){
       Some(t1)
      }
      else if(t1 != null &amp;&amp; t2 != null &amp;&amp; t2.before(t1)){
       Some(t2)
      } 
      else if(t1 != null){
       Some(t1)
      } 
       else if(t2 != null){
       Some(t2)
      } 
      else {
         None
      }
    })
   
  
  var df = Seq((1L, &quot;2021-01-04 16:10:00&quot;,&quot;2021-01-04 15:20:00&quot;)).toDF(&quot;id&quot;,&quot;t1&quot;,&quot;t2&quot;)
  df = (df.withColumn(&quot;t1&quot;,to_timestamp($&quot;t1&quot;,&quot;yyyy-MM-dd HH:mm:ss&quot;))
        .withColumn(&quot;t2&quot;,to_timestamp($&quot;t2&quot;,&quot;yyyy-MM-dd HH:mm:ss&quot;)))
  
  df = df.withColumn(&quot;t3&quot;,compareTime($&quot;t1&quot;,$&quot;t2&quot;))
  df.show()
  </code></pre>