Spark从另一个国家获取当前日期

时间:2019-11-24 13:24:09

标签: apache-spark pyspark

我需要获取另一个国家的日期和时间:

dateFormat = "%Y%m%d_%H%M"
ts=spark.sql(""" select current_timestamp() as ctime """).collect()[0]["ctime"]
ts.strftime(dateFormat)

2 个答案:

答案 0 :(得分:2)

您不需要pyspark来完成这样的任务,尤其是当您调用.collect()时:

import pytz
from datetime import datetime

tz = pytz.timezone('Asia/Shanghai')
ts = datetime.now(tz)
ts.strftime('%Y%m%d_%H%M')

答案 1 :(得分:0)

会话时区设置为'spark.sql.session.timeZone',默认为JVM系统本地时区,您可以更改时区,添加时区会给您正确的日期

        spark.conf.set("spark.sql.session.timeZone", "UTC")
相关问题