Spark SQL - Conversion between UTC and Timestamp with Time Zone

visibility 153 access_time 24 days ago languageEnglish timeline Stats
timeline Stats
Page index 6.04
more_horiz

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.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

More from Kontext
Spark SQL - Make Date, Timestamp and Intervals
visibility 19
thumb_up 0
access_time 12 days ago
Spark SQL - flatten Function
visibility 19
thumb_up 0
access_time 28 days ago
Spark SQL - flatten Function