G G
G G

Reputation: 1069

How to get date and time from string?

I have a data frame with the following schema:

  root

  |-- date : string (nullable = true)

The value looks like 201605250000. How can I extract date, hour and mm from this string?

Upvotes: 6

Views: 4263

Answers (1)

zero323
zero323

Reputation: 330113

Parse string:

val unix = unix_timestamp($"date", "yyyyMMddHHmm").alias("unix")

Convert to timestmap:

val ts = unix.cast("timestamp").alias("ts")

Cast to date to get a date:

val dt = ts.cast("date").alias("dt")

Use hour / minute to get time:

val h = hour(ts).alias("h")
val m = minute(ts).alias("m")

Example:

import org.apache.spark.sql.functions._

val df = Seq((1L, "201605250000")).toDF("id", "date")
df.select($"*", unix, ts, dt, h, m).show

// +---+------------+----------+--------------------+----------+---+---+
// | id|        date|      unix|                  ts|        dt|  h|  m|
// +---+------------+----------+--------------------+----------+---+---+
// |  1|201605250000|1464127200|2016-05-25 00:00:...|2016-05-25|  0|  0|
// +---+------------+----------+--------------------+----------+---+---+

Note: For 1.5 use unix.cast("double").cast("timestamp")

Upvotes: 8

Related Questions