In this tutorial, we will show you a Spark SQL example of how to convert String to Date format using to_date() function on the DataFrame column with Scala example.
Note that Spark Date Functions support all Java Date formats specified in DateTimeFormatter.
to_date() - function is used to format string (StringType) to date (DateType) column.
Syntax: to_date(dateColumn:Column,format:String) : Column
Below code, snippet takes the date in a string and converts it to date format on DataFrame.
Seq(("06-03-2009"),("07-24-2009")).toDF("Date").select(
col("Date"),
to_date(col("Date"),"MM-dd-yyyy").as("to_date")
).show()
Output:
+----------+----------+
| Date| to_date|
+----------+----------+
|06-03-2009|2009-06-03|
|07-24-2009|2009-07-24|
+----------+----------+
<a id="h-complete-example"></a>
Complete Example
package com.aistudylabs.spark.dataframe.functions.datetime
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.{col, to_date}
object StringToDate extends App {
val spark:SparkSession = SparkSession.builder()
.master("local")
.appName("AIStudyLabs.com")
.getOrCreate()
spark.sparkContext.setLogLevel("ERROR")
import spark.sqlContext.implicits._
Seq(("06-03-2009"),("07-24-2009")).toDF("Date").select(
col("Date"),
to_date(col("Date"),"MM-dd-yyyy").as("to_date")
).show()
}
<a id="h-conclusion"></a>
Conclusion:
In this article, you have learned how to convert String to Date format using Date functions.
<a id="h-related-articles"></a>
Related Articles
- Spark date_format() – Convert Timestamp to String
- Spark date_format() – Convert Date to String format
- Spark – How to update the DataFrame column?
- Spark convert Unix timestamp (seconds) to Date
- Spark to_date() – Convert timestamp to date
- Spark – How to get current date & timestamp
- Spark SQL – Truncate Date Time by unit specified
- Spark SQL Date and Timestamp Functions
