date_format无法处理带有'00:00:00`的时间戳记

问题描述 投票:1回答:1

2020-01-27 00:00:00类型的timestamp格式化为2020-01-27 12:00:00,而不是2020-01-27 00:00:00

  import spark.sqlContext.implicits._
  import java.sql.Timestamp
import org.apache.spark.sql.functions.typedLit


scala>   val stamp = typedLit(new Timestamp(1580105949000L))
stamp: org.apache.spark.sql.Column = TIMESTAMP('2020-01-27 00:19:09.0')


scala>   var df_test = Seq(5).toDF("seq").select(
     |     stamp.as("unixtime"),
     |     date_trunc("HOUR", stamp).as("date_trunc"),
     |     date_format(date_trunc("HOUR", stamp), "yyyy-MM-dd hh:mm:ss").as("hour")
     |   )
df_test: org.apache.spark.sql.DataFrame = [unixtime: timestamp, date_trunc: timestamp ... 1 more field]


scala> df_test.show
+-------------------+-------------------+-------------------+
|           unixtime|         date_trunc|               hour|
+-------------------+-------------------+-------------------+
|2020-01-27 00:19:09|2020-01-27 00:00:00|2020-01-27 12:00:00|
+-------------------+-------------------+-------------------+
scala apache-spark datetime unix-timestamp
1个回答
0
投票

您的模式应为yyyy-MM-dd HH:mm:ss

date_format,根据its documentation,使用java.text.SimpleDateFormat支持的说明符:

将日期/时间戳记/字符串转换为第二个参数给出的日期格式指定的格式的字符串值。有关有效的日期和时间格式模式,请参见SimpleDateFormat。

[SimpleDateFormat的文档可以在here]中找到>

hh用于“上午/下午(1-12)的小时数”。您正在寻找一天中的小时指示符,即HH

© www.soinside.com 2019 - 2024. All rights reserved.