【问题标题】:convert integer into date to count number of days将整数转换为日期以计算天数
【发布时间】:2018-11-24 13:39:27
【问题描述】:

我需要将 Integer 转换为 date(yyyy-mm-dd) 格式,以计算天数。

  registryDate 
    20130826
    20130829
    20130816
    20130925
    20130930
    20130926

期望的输出:

 registryDate      TodaysDate        DaysInBetween
    20130826        2018-11-24          1916
    20130829        2018-11-24          1913
    20130816        2018-11-24          1926

【问题讨论】:

    标签: scala apache-spark apache-spark-sql user-defined-functions apache-spark-dataset


    【解决方案1】:

    您可以将registryDate 转换为String 类型,然后应用to_datedatediff 计算天差,如下所示:

    import org.apache.spark.sql.functions._
    import org.apache.spark.sql.types._
    import java.sql.Date
    
    val df = Seq(
      20130826, 20130829, 20130816, 20130825
    ).toDF("registryDate")
    
    df.
      withColumn("registryDate2", to_date($"registryDate".cast(StringType), "yyyyMMdd")).
      withColumn("todaysDate", lit(Date.valueOf("2018-11-24"))).
      withColumn("DaysInBetween", datediff($"todaysDate", $"registryDate2")).
      show
    // +------------+-------------+----------+-------------+
    // |registryDate|registryDate2|todaysDate|DaysInBetween|
    // +------------+-------------+----------+-------------+
    // |    20130826|   2013-08-26|2018-11-24|         1916|
    // |    20130829|   2013-08-29|2018-11-24|         1913|
    // |    20130816|   2013-08-16|2018-11-24|         1926|
    // |    20130825|   2013-08-25|2018-11-24|         1917|
    // +------------+-------------+----------+-------------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2021-08-17
      • 2018-10-16
      • 2022-07-07
      相关资源
      最近更新 更多