title: cast the string column to date for the particular format
body: <p>How to cast the string column to date column and maintain the same format in spark data frame?</p>
  
  <p>I want to cast the string column to date by specifying the format, but the after cast date always comes in the default format which is yyyy-MM-dd.</p>
  
  <p>But I want the Date type with the format which is in the string value(I want the data type as Date only not as String)</p>
  
  <p>For example:</p>
  
  <pre><code> val spark = SparkSession.builder().master("local").appName("appName").getOrCreate()
   import spark.implicits._
   //here the format is MMddyyyy(For Col2 which is of String type here)
   val df = List(("1","01132019"),("2","01142019")).toDF("Col1","Col2")
  
   import org.apache.spark.sql.functions._
  
   //Here i need the Col3 in Date type and with the format MMddyyyy But it is converting into yyyy-MM-dd
    val df1 = df.withColumn("Col3",to_date($"Col2","MMddyyyy"))
  
   //I tried this but this will give me Col3 in String data type which i need in Date
    val df1 = df.withColumn("Col3",date_format(to_date($"Col2","MMddyyyy"),"MMddyyyy"))
  </code></pre>