Created
September 6, 2019 22:47
-
-
Save buildlackey/62333617980da38d192164a5b5347b1a to your computer and use it in GitHub Desktop.
convertAndShow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def updateTimeZone(tz: String, sessionTz: String) = { | |
import java.time._ | |
import java.util.TimeZone | |
System.setProperty("user.timezone", tz); | |
TimeZone.setDefault(TimeZone.getTimeZone(tz)) | |
spark.conf.set("spark.sql.session.timeZone", sessionTz) | |
} | |
def convertAndShow(timeStr: String) = { | |
val df = List(timeStr ).toDF("timestr"). | |
withColumn("to_timestamp_ts", to_timestamp(col("timestr"))). | |
withColumn("to_timestamp_tsInt", col("to_timestamp_ts").cast("integer")). | |
withColumn("cast_ts", col("timestr").cast("timestamp")). | |
withColumn("cast_tsInt", col("cast_ts").cast("integer")) | |
df.show(false) | |
} | |
updateTimeZone("PST", "PST") | |
convertAndShow( "1970-01-01T00:00:00-01:00" ) | |
// Resulting output is: | |
//+-------------------------+-------------------+------------------+-------------------+----------+ | |
//|timestr |to_timestamp_ts |to_timestamp_tsInt|cast_ts |cast_tsInt| | |
//+-------------------------+-------------------+------------------+-------------------+----------+ | |
//|1970-01-01T00:00:00-01:00|1969-12-31 17:00:00|3600 |1969-12-31 17:00:00|3600 | | |
//+-------------------------+-------------------+------------------+-------------------+----------+ | |
// | |
convertAndShow( "1970-01-01T00:00:00-00:00" ) | |
// Resulting output is: | |
//+-------------------------+-------------------+------------------+-------------------+----------+ | |
//|timestr |to_timestamp_ts |to_timestamp_tsInt|cast_ts |cast_tsInt| | |
//+-------------------------+-------------------+------------------+-------------------+----------+ | |
//|1970-01-01T00:00:00-00:00|1969-12-31 16:00:00|0 |1969-12-31 16:00:00|0 | | |
//+-------------------------+-------------------+------------------+-------------------+----------+ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment