Spark SQL - Conversion between UTC and Timestamp with Time Zone
In Spark SQL, function from_utc_timestamp(timestamp, timezone)
converts UTC timestamp to a timestamp in the given time zone; function to_utc_timestamp(timestamp, timezone)
converts timestamp in a given time zone to UTC timestamp.
Code snippets
The following are some examples to convert UTC to the time with time zone information and vice versa.
spark-sql> select from_utc_timestamp('2022-06-04 21:34:29.538','Australia/Melbourne'); from_utc_timestamp(CAST(2022-06-04 21:34:29.538 AS TIMESTAMP), Australia/Melbourne) 2022-06-05 07:34:29.538 spark-sql> select to_utc_timestamp('2022-06-04 21:34:29.538','UTC+10'); to_utc_timestamp(CAST(2022-06-04 21:34:29.538 AS TIMESTAMP), UTC+10) 2022-06-04 11:34:29.538 spark-sql> select to_utc_timestamp('2022-06-04 21:34:29.538','Australia/Melbourne'); to_utc_timestamp(CAST(2022-06-04 21:34:29.538 AS TIMESTAMP), Australia/Melbourne) 2022-06-04 11:34:29.538
Time zone ID list
You can find the complete list of time zone ID here:List of tz database time zones - Wikipedia
You can also use GMT and UTC timezone string, for example, 'GMT+1', 'UTC+10', etc.
copyright
This page is subject to Site terms.
comment Comments
No comments yet.
Log in with external accounts
warning Please login first to view stats information.
article
Spark SQL - Extract Value from JSON String
article
Spark SQL - Make Date, Timestamp and Intervals
article
Spark SQL - Left and Right Padding (lpad and rpad) Functions
code
PySpark DataFrame - Convert JSON Column to Row using json_tuple
article
Spark Hash Functions Introduction - MD5 and SHA
Read more (46)