Convert String to Date in Spark (Scala)

22 views 0 comments last modified about 17 days ago Raymond Tang

lite-log scala


This pages demonstrates how to convert string to java.util.Date in Spark via Scala.


If you have not installed Spark, follow the page below to install it:

Install Big Data Tools (Spark, Zeppelin, Hadoop) in Windows for Learning and Practice

Sample code

The following code snippet uses pattern yyyy-MM-dd to parse string to Date.

import java.text.SimpleDateFormat

import java.util.Date

val format = new SimpleDateFormat("yyyy-MM-dd")

val date = format.parse("2018-03-03")



Retrieve week day name

scala> val format2 = new SimpleDateFormat("EEEEE")
format2: java.text.SimpleDateFormat = java.text.SimpleDateFormat@3ecbf05

scala> format2.format(date)
res0: String = Saturday


For the complete list of Java date and time pattern, please refer to the following link.

Add comment

Comments (0)

No comments yet.