You are currently viewing Spark Epoch time to timestamp and Date

In this Spark article, you will learn how to convert or cast Epoch time to Timestamp and Date using SQL function from_unixtime() and Scala language

What is Epoch Time

Epoch time is also known as Unix time which specifies the moment in time since 1970-01-01 00:00:00 UTC. It is the number of seconds passed since Epoch time. Epoch time is widely used in Unix like operating systems.

1. Convert Epoch time to timestamp

from_unixtime() SQL function is used to convert or cast Epoch time to timestamp string and this function takes Epoch time as a first argument and formatted string time as the second argument. As a first argument, we use unix_timestamp() which returns the current timestamp in Epoch time (Long) as an argument.

  //Convert unix timestamp to timestamp
  val timeDF = Seq(1).toDF("seq").select(
    from_unixtime(unix_timestamp(),"MM-dd-yyyy HH:mm:ss").as("timestamp_2"),
    from_unixtime(unix_timestamp(),"dd-MM-yyyy HH:mm:ss").as("timestamp_3"),

Yields below output

// Output:
|        timestamp_1|        timestamp_2|        timestamp_3|timestamp_4|
|2019-12-24 10:36:12|12-24-2019 10:36:12|24-12-2019 10:36:12|   10:36:12|

1. Convert Epoch time to Date

Similar to above, here we use the same from_unixtime() SQL function to convert Epoch time to date besides we need to provide formatted string date as a second argument to the function

  // Convert unix timestamp to date
  val dateDF = Seq(1).toDF("seq").select(
    from_unixtime(unix_timestamp(),"dd-MM-yyyy HH:mm:ss").as("date_2"),

Yields below output

// Output:
|date_1    |date_2             |date_3    |
|12-24-2019|24-12-2019 10:36:12|2019-12-24|

The complete example is available at GitHub project for reference


In this Spark article, you have learned how to convert or cast the Epoch time to Date or Timestamp using from_unixtime() function along with Scala example.

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium