Apache Spark 如何将日期时间从 Australia/Melbourne 时间转换为 UTC?

Apache Spark How to convert a datetime from Australia/Melbourne time to UTC?

如何在 Scala 的 Apache Spark 中将 Australia/Melbourne 时间的 21/10/2021 15:15:28 之类的日期时间字符串转换为 UTC?

试试这个(当然你可以忽略数据的创建,我添加它是为了让你理解流程:列的名称和类型):

Seq("21/10/2021 15:15:28").toDF("timeStr")
      .withColumn("australiaMelbourneTime", to_timestamp(col("timeStr"), "dd/MM/yyyy HH:mm:ss"))
      .withColumn("utcTime", to_utc_timestamp(col("australiaMelbourneTime"), "Australia/Melbourne"))

输出(测试):

+-------------------+----------------------+-------------------+
|            timeStr|australiaMelbourneTime|            utcTime|
+-------------------+----------------------+-------------------+
|21/10/2021 15:15:28|   2021-10-21 15:15:28|2021-10-21 04:15:28|
+-------------------+----------------------+-------------------+