简体   繁体   中英

Date_format function in hive

I ran basic select statement select date_format('2021-12-27','YYYYMMdd') in hive expecting "20211227" but hive return 20221227 but I run select date_format('2021-12-25','YYYYMMdd') , it gives me expected result "20211225. Not sure, why hive behave this way.

hive uses Java simpledateformat. As per Java guide,

  • y (lowercase) is year
  • Y (uppercase) is 'week-based-year'

This difference will cause your code to work perfectly fine, except for when dealing with dates at the very end of some years. So, when you use ('2021-12-27','YYYYMMdd'), yyyy will output 2021 but YYYY will output 2022. Because the week that the 27th of December falls in the first week of 2022.
date_format('2021-12-25','YYYYMMdd') is working because 25th Dec is not the first week of 2022.
Refer to below screenshot, you can see lowercase y is giving you correct result and uppercase is picking up year from first week of year which is 2022.

例子

Please always use yyyy as per hive docs says.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM