Spark SQL - UNIX timestamp functions

Raymond Tang Raymond Tang 0 4472 2.74 index 1/9/2021

Current UNIX timestamp

Function unix_timestamp() returns the UNIX timestamp of current time. You can also specify a input timestamp value.

Example:

spark-sql> select unix_timestamp();
unix_timestamp(current_timestamp(), yyyy-MM-dd HH:mm:ss)
1610174099

spark-sql> select unix_timestamp(current_timestamp, 'yyyy-MM-dd');
unix_timestamp(current_timestamp(), yyyy-MM-dd)
1610174365

UNIX timestamp string to date

Function from_unixtime(unix_time, format) can be used to convert UNIX time to Spark SQL date data type.

Example:

spark-sql> select from_unixtime(1610174365, 'yyyy-MM-dd');
from_unixtime(CAST(1610174365 AS BIGINT), yyyy-MM-dd)
2021-01-09

UNIX timestamp string to timestamp

Function from_unixtime(unix_time, format) can also be used to convert UNIX time to Spark SQL timestamp data type.

Example:

spark-sql> select from_unixtime(1610174099, 'yyyy-MM-dd HH:mm:ss');
from_unixtime(CAST(1610174099 AS BIGINT), yyyy-MM-dd HH:mm:ss)
2021-01-09 17:34:59
spark-sql spark-sql-function

Join the Discussion

View or add your thoughts below

Comments