hive日期函数处理
生活随笔
收集整理的這篇文章主要介紹了
hive日期函数处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 日期函數unix時間轉日期函數:from_unixtime,語法為from_unixtime(bigint unixtime, stringformat)
select from_unixtime('1323308943','yyyyMMdd') --201112082. 獲取unixtime:current_timestamp()
select current_timestamp(from_unixtime())3. 獲取當前日期
select current_date()4. 日期相減得到天數差:datediff(enddate,startdate)
5. 日期加減:date_add(date,add_days), date_sub(date,sub_days)
6. 轉成日期:
select to_date('2018-02-27 10:03:01') ; --2018-02-27 7 --當月最后一天 select last_day('2018-02-27 10:03:01'); --2018-02-28--當月第一天 select trunc(current_date,'MM') as day; --2020-11-01--當年第一天 select trunc(current_date,'YY') as day; --2020-01-01--next_day,返回當前時間的下一個星期幾所對應的日期 select next_day('2018-02-27 10:03:01', 'TU'); selectday -- 時間,date_add(day,1 - dayofweek(day)) as week_first_day -- 本周第一天_周日,date_add(day,7 - dayofweek(day)) as week_last_day -- 本周最后一天_周六,date_add(day,1 - case when dayofweek(day) = 1 then 7 else dayofweek(day) - 1 end) as week_first_day -- 本周第一天_周一,date_add(day,7 - case when dayofweek(day) = 1 then 7 else dayofweek(day) - 1 end) as week_last_day -- 本周最后一天_周日,next_day(day,'TU') as next_tuesday -- 當前日期的下個周二,trunc(day,'MM') as month_first_day -- 當月第一天,last_day(day) as month_last_day -- 當月最后一天,to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3 -2,2,0),'-01')) as season_first_day -- 當季第一天,last_day(to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3,2,0),'-01'))) as season_last_day -- 當季最后一天,trunc(day,'YY') as year_first_day -- 當年第一天,last_day(add_months(trunc(day,'YY'),12)) as year_last_day -- 當年最后一天,weekofyear(day) as weekofyear -- 當年第幾周,second(day) as second -- 秒鐘,minute(day) as minute -- 分鐘,hour(day) as hour -- 小時,day(day) as day -- 日期,month(day) as month -- 月份,lpad(ceil(month(day)/3),2,0) as season -- 季度,year(day) as year -- 年份 from (select '2018-01-02 01:01:01' as day union allselect '2018-02-02 02:03:04' as day union allselect '2018-03-02 03:05:07' as day union allselect '2018-04-02 04:07:10' as day union allselect '2018-05-02 05:09:13' as day union allselect '2018-06-02 06:11:16' as day union allselect '2018-07-02 07:13:19' as day union allselect '2018-08-02 08:15:22' as day union allselect '2018-09-02 09:17:25' as day union allselect '2018-10-02 10:19:28' as day union allselect '2018-11-02 11:21:31' as day union allselect '2018-12-02 12:23:34' as day ) t1 ;spark字符串日期yyyymmdd計算天數差
df.withColumn("time",(unix_timestamp(col("dt"), "yyyyMMdd")-unix_timestamp(lit("20210926"), "yyyyMMdd"))/(3600*24)).show(10,false)
總結
以上是生活随笔為你收集整理的hive日期函数处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Photoshop中图层叠加效果的算法
- 下一篇: Hive时间日期函数一文详解+代码实例