title: Pyspark - Convert mmddyy to YYYY-MM-DD
body: <p>I am working on a large file, which has one of the field in mmddyy format having string as datatype and I need to convert it into YYYY-MM-DD. I did tried creating UDF and convert referring to one of the post but its throwing error. Sample code:</p>
  
  <p>Actual field in dataframe:</p>
  
  <pre><code>+-----------+
  |DATE_OPENED|
  +-----------+
  |     072111|
  |     090606|
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>+---------------+
  |    DATE_OPENED|
  +---------------+
  |     2011-07-21|
  |     2006-06-09|
  </code></pre>
  
  <p>Sample Code:</p>
  
  <pre><code>func =  udf (lambda x: datetime.strptime(x, '%m%d%Y'), DateType())
  
  newdf = olddf.withColumn('open_dt' ,date_format(func(col('DATE_OPENED')) , 'YYYY-MM-DD'))
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Error : ValueError: time data '072111' does not match format '%m%d%Y'
  </code></pre>