【问题标题】:HQL (HIVE) date_format adding an extra year after Dec 25th 2021HQL (HIVE) date_format 在 2021 年 12 月 25 日之后增加一年
【发布时间】:2021-12-18 19:33:52
【问题描述】:

我不确定这是否只是在我的本地实例上,或者它是否是一个基本问题,但对我来说,hive date_format 在 2021 年 12 月 25 日之后一年跳跃。

Select date_format('2021-12-25','YYYY-MM-dd') as Correct2021
        ,date_format('2021-12-26','YYYY-MM-dd') as Wrong2022
        ,date_format(date_add(current_date,51),'YYYY-MM-dd') as Correct2021b
        ,date_format(date_add(current_date,52),'YYYY-MM-dd') as Wrong2022b

我上面的代码返回

correct2021 wrong2022 correct2021b wrong2022b
2021-12-25 2022-12-26 2021-12-25 2022-12-26

如果在 2021 年 11 月 4 日之后的其他日期运行,您需要调整添加的日期。

【问题讨论】:

    标签: sql date hive hiveql simpledateformat


    【解决方案1】:

    原来这是'Y'与'y'的java问题。

    这里描述了这个问题: Y returns 2012 while y returns 2011 in SimpleDateFormat

    【讨论】:

      【解决方案2】:

      Y 模式是一周年 - 某周所属的一年。

      y - 是你需要的

      Select date_format('2021-12-25','yyyy-MM-dd') as Correct2021
              ,date_format('2021-12-26','yyyy-MM-dd') as Wrong2022
              ,date_format(date_add(current_date,51),'yyyy-MM-dd') as Correct2021b
              ,date_format(date_add(current_date,52),'yyyy-MM-dd') as Wrong2022b
      

      另见https://stackoverflow.com/a/69840917/2700344

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-16
        • 2023-01-20
        • 2021-09-01
        相关资源
        最近更新 更多