当前位置:   article > 正文

Hive常用的日期函数_hive获取年初日期

hive获取年初日期

1.获取当前日期、时间、时间戳、时间戳对应日期

  1. -- 代码 --
  2. select current_date() -- 当前日期
  3. ,current_timestamp() -- 当前默认时间
  4. ,from_utc_timestamp(current_timestamp(),'GMT+8') -- 转为东八区时间
  5. ,unix_timestamp() -- 时间戳
  6. ,from_unixtime(unix_timestamp()) -- 时间戳对应时间(东八区)
  7. ,to_utc_timestamp(from_unixtime(unix_timestamp()),'GMT') -- 当前时间戳转为时间(默认时区)
  8. -- 结果 --
  9. 2023-02-01
  10. 2023-02-01 09:57:43 -- (时间差8h)
  11. 2023-02-01 17:57:43
  12. 1675245463
  13. 2023-02-01 17:57:43
  14. 2023-02-01 09:57:43

注意:

current_timestamp() 获取的时UTC默认时区。

给定一个时间戳可基于from_utc_timestamp/to_utc_timestamp进行转换。

2.将日期转换为时间戳

  1. 代码:select unix_timestamp('2023-01-20 10:25:30') as dt
  2. 结果:1674181530

3.将时间戳转换为日期

  1. -- 代码 --
  2. select
  3. from_unixtime(1674181530) as dt1
  4. ,from_unixtime(1674181530, 'yyyy-MM-dd') as dt2
  5. ,from_unixtime(1674181530, 'yyyyMMdd') as dt3
  6. -- 结果 --
  7. 2023-01-20 10:25:30
  8. 2023-01-20
  9. 20230120

4.日期格式转换

  1. -- 代码 --
  2. select date_format('2023-01-20 10:25:30','yyyy-MM-dd') as dt1
  3. ,date_format('2023-01-20 10:25:30','yyyyMMdd') as dt2
  4. -- 结果 --
  5. 2023-01-20
  6. 20230120

5.获取年、月、日、天、小时、分钟、秒、周数、星期几、季节函数

  1. -- 代码 --
  2. select year('2023-01-20 10:25:30') as year
  3. ,month('2023-01-20 10:25:30') as month
  4. ,day('2023-01-20 10:25:30') as day
  5. ,hour('2023-01-20 10:25:30') as hour
  6. ,minute('2023-01-20 10:25:30') as minute
  7. ,second('2023-01-20 10:25:30') as second
  8. ,weekofyear('2023-01-20 10:25:30') as weekofyear
  9. -- ,dayofweek('2023-01-20 10:25:30') as dayofweek
  10. ,date_format('2023-01-20 10:25:30' ,'u') as dayofweek2
  11. ,ceil(month('2023-01-20 10:25:30')/3) as season
  12. -- 结果 --
  13. 2023
  14. 1
  15. 20
  16. 10
  17. 25
  18. 30
  19. 3 -- 此处为3,而非4
  20. 5
  21. 1

注意:

   如果当前年的第一个周,天数超过3天,那就是当前年的第一周;

   如果当前年的第一个周,天数小于等于3天,那就是上一年的最后一周。

weekofyear('2023-01-01') 结果为52;

weekofyear('2023-01-02') 结果为1.

6.日期/月份的加减函数

  1. -- 代码 --
  2. select date_add('2023-01-20',1) as dt_add1
  3. ,date_sub('2023-01-20',1) as dt_sub1
  4. ,add_months('2023-01-20',1) as dt_add_month1
  5. ,add_months('2023-01-20',-1) as dt_sub_month1
  6. -- 结果 --
  7. 2023-01-21
  8. 2023-01-19
  9. 2023-02-20
  10. 2022-12-20

7.日期小时/分钟的加减函数

  1. -- 代码 --
  2. select from_unixtime(unix_timestamp('2023-01-20 10:25:30')- 1*3600) as sub_hour1 -- 减1小时
  3. ,from_unixtime(unix_timestamp('2023-01-20 10:25:30') + 1*3600) as add_hour1 -- 加1小时
  4. ,from_unixtime(unix_timestamp('2023-01-20 10:25:30') - 10*60) as sub_minute10 --减10分钟
  5. ,from_unixtime(unix_timestamp('2023-01-20 10:25:30') + 10*60) as sub_minute10 --加10分钟
  6. -- 结果 --
  7. 2023-01-20 09:25:30
  8. 2023-01-20 11:25:30
  9. 2023-01-20 10:15:30
  10. 2023-01-20 10:35:30

8.两个日期(月份)相减函数

  1. -- 代码1 -- 跨月数据
  2. select day_diff1,month_diff1,day_diff2,month_diff2,day_diff2/day_diff1 as month_diff
  3. from (
  4. select datediff('2023-01-20', '2022-12-20') as day_diff1 -- 结束日期在前
  5. ,months_between('2023-01-20', '2022-12-20') as month_diff1
  6. ,datediff('2023-01-20', '2022-12-21') as day_diff2 -- 结束日期在前
  7. ,months_between('2023-01-20', '2022-12-21') as month_diff2
  8. )
  9. -- 结果 --
  10. 31
  11. 1.0
  12. 30
  13. 0.9677
  14. 0.9677
  15. -- 代码2 -- 当月数据
  16. select datediff('2023-01-20', '2023-01-10') as day_diff1 -- 结束日期在前
  17. ,months_between('2023-01-20', '2023-01-10') as month_diff1
  18. ,10/31 as c1
  19. ,datediff('2023-02-20', '2023-02-10') as day_diff2 -- 结束日期在前
  20. ,months_between('2023-02-20', '2023-02-10') as month_diff2
  21. ,10/28 as c2
  22. ,10/31 as c3
  23. -- 结果 --
  24. 10
  25. 0.32258
  26. 0.32258
  27. 10
  28. 0.32258
  29. 0.35714
  30. 0.32258

注意:

months_between使用时:

  若是跨月日期,则分母按照跨月整月天数(31/30/28)计算;

  若是同月日期,则分母统一使用31天进行计算。

9.获取年初(年末)、月初(月末)、周一(周日)

  1. -- 代码 --
  2. select
  3. trunc('2023-01-20', 'YY') as year_first -- 年初
  4. ,date_sub(add_months(trunc('2023-01-20', 'YY'),12),1) as year_end -- 年末
  5. ,trunc('2023-01-20','MM') as month_first -- 月初
  6. ,last_day('2023-01-20') as month_end -- 月末
  7. ,date_sub(next_day('2023-01-20','Mon'),7) as monday -- 周一
  8. ,date_sub(next_day('2023-01-20','Mon'),1) as sunday -- 周日
  9. -- 结果 --
  10. 2023-01-01
  11. 2023-12-31
  12. 2023-01-01
  13. 2023-01-31
  14. 2023-01-16
  15. 2023-01-22

10.获取当前日期的下个星期几(并非下周几)的日期

  1. -- 代码 --
  2. select
  3. next_day('2023-01-20', 'MO') as next_mo
  4. ,next_day('2023-01-20', 'TU') as next_tu
  5. ,next_day('2023-01-20', 'WE') as next_we
  6. ,next_day('2023-01-20', 'TH') as next_th
  7. ,next_day('2023-01-20', 'FR') as next_fr
  8. ,next_day('2023-01-20', 'SA') as next_sa
  9. ,next_day('2023-01-20', 'SU') as next_su
  10. -- 结果 --
  11. 2023-01-23
  12. 2023-01-24
  13. 2023-01-25
  14. 2023-01-26
  15. 2023-01-27
  16. 2023-01-21 -- 本周六
  17. 2023-01-22 -- 本周日
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/965633
推荐阅读
相关标签
  

闽ICP备14008679号