Convert String to Date in Spark (Scala)

visibility 10,141 access_time 2 years ago languageEnglish

Context

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

Prerequisites

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")

Output

image

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

Summary

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

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

info Last modified by Raymond 2 years ago copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

timeline Stats
Page index 6.59
More from Kontext
Write and Read Parquet Files in Spark/Scala
visibility 29,633
thumb_up 0
access_time 2 years ago
Spark Scala: Load Data from SQL Server
visibility 1,758
thumb_up 0
access_time 2 years ago
Spark Scala: Load Data from MySQL
visibility 1,461
thumb_up 0
access_time 2 years ago
Spark Scala: Load Data from MySQL