简介
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
--查看所有方法SHOW FUNCTIONS;--查看方法声明DESCRIBE FUNCTION <function_name>;--查看方法的详细使用说明DESCRIBE FUNCTION EXTENDED <function_name>;
常用方法整理
from_unixtime
语法:from_unixtime(bigint unixtime[, string format])返回值:string说明:转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格eg:select from_unixtime(cast(current_timestamp() as bigint),'yyyy-MM-dd');2021-03-25
unix_timestamp
一 语法:unix_timestamp(string date)返回值:bigint说明:传入一个yyyy-MM-dd HH:mm:ss类型的时间字符串,返回一个以系统时区转换后的时间戳eg:select unix_timestamp("2021-03-24 23:30:30");1616599830二 语法:unix_timestamp(string date, string pattern);返回值:bigint说明:传入一个字符串时间,和格式化类型,返回一个转换后的时间戳eg:select unix_timestamp("2021-03-24 23:30:30","yyyy-MM-dd");1616515200
to_date
语法:to_date(string timestamp)返回值:2.1之前返回字符串,2.1之后返回date类型说明:select to_date("1970-01-01 00:00:00");"1970-01-01"
year
语法:year(string date)返回值:int说明:返回传入时间的年份eg:select year("1970-01-01 00:00:00");1970select year("1970-01-01") ;1970
month
语法:month(string date)返回值:int说明:获取传入时间的月份部分eg:select month("1970-11-01 00:00:00") ;11select month("1970-11-01") ;11
day
一 语法:day(string date)返回值:int说明:获取传入时间的日部分eg:select day("1970-11-01 00:00:00") ;1select tday("1970-11-01") ;1二 语法:dayofmonth(date)返回值:int说明:获取传入时间的日部分eg:select dayofmonth("2021-02-01");1select dayofmonth("2021-02-01 12:00:00");1
hour
语法:hour(string date)返回值:int说明:获取传入时间的小时部分eg:select hour('2009-07-30 12:58:59') ;12select hour('12:58:59');12
datediff
语法:datediff(string enddate, string startdate);返回值:int说明:返回日期的差值eg:select datediff('2009-03-01', '2009-02-27') ;2
date_add
语法:date_add(date/timestamp/string startdate, tinyint/smallint/int days)返回值:int说明:在基础日期上加上指定天数,天数可为负eg:select date_add("2021-03-25",1);2021-03-26select date_add("2021-03-25",-1);2021-03-24
date_sub
语法:date_sub(date/timestamp/string startdate, tinyint/smallint/int days)返回值:int说明:日期减函数,天数值可为负eg:select date_sub("2021-03-25",-1);2021-03-26select date_sub("2021-03-25",1);2021-03-24
date_format
语法:date_format(date/timestamp/string ts, string fmt)返回值:string说明:日期格式化函数eg:select date_format(current_timestamp(),'yyyy-MM-dd');2021-03-25select date_format("2021-02-01 12:30:20",'yyyy/MM/dd hh:mm:ss');2021/02/01 12:30:20日期格式化类型官网介绍:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
quarter
语法:quarter(date/timestamp/string)返回值:int说明:返回当前季度 1-4 之间的值eg: select quarter('2015-04-08') ;2
extract
语法:extract(field FROM source)返回值:int说明:时间信息提取,可提取值day, dayofweek, hour, minute, month, quarter, second, week and yeareg: select extract(day from "2021-03-01");1select extract(dayofweek from "2021-03-01");2select extract(month from "2021-03-01");3
current_date
语法:current_date返回值:date说明:返回当前时间eg: select current_date;2021-03-24
current_timestamp
语法:current_timestamp返回值:timestamp说明:返回当前时间戳类型eg: select current_timestamp;2021-03-24 10:48:22.103
add_months
语法:add_months(string start_date, int num_months, output_date_format)返回值:string说明:月份加减函数eg: select add_months("2021-02-01",1,"yyyyMMdd");20210301select add_months("2021-02-01",-1,"yyyyMMdd");20210101
last_day
语法:last_day(string date)返回值:string说明:返回本月的最后一天 date is a string in the format 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. 其它格式的输入将会被忽略eg: select last_day("2021-03-01");2021-03-31select last_day("2021-03-01 12:30:23");2021-03-31select last_day("2021/03/01 12:30:23"); 格式不支持NULL
next_day
语法:next_day(string start_date, string day_of_week)返回值:string说明:day_of_week 可选参数为星期一到周天的英文单词星期一Monday简写为Mon,星期二Tuesday简写为Tue,星期三Wednesday简写为Wed,星期四Thursday简写为Thu,星期五Friday简写为Fri,星期六Saturday简写为Sat,星期日Sunday简写为Suneg: 取当前日期的下一个星期一;SELECT next_day('2021-03-01', 'Mon');2021-03-08SELECT next_day('2021-03-01', 'Monday');2021-03-08
trunc
语法:trunc(string date, string format)返回值:string说明:初始化指定格式的日期eg:select trunc('2021-03-24','YY');2021-01-01select trunc('2021-03-24','MM');2021-03-01
组合案例
获取上月月初时间
select add_months(trunc('2021-03-24','MM'),-1,'yyyy-MM-dd');2021-02-01
获取上月月末时间
select last_day(add_months('2021-03-24',-1,'yyyy-MM-dd'));2021-02-28
获取下月月初时间
select add_months(trunc('2021-03-24','MM'),1,'yyyy-MM-dd');2021-04-01
获取下月月末时间
select last_day(add_months('2021-03-24',1,'yyyy-MM-dd'));2021-04-30
时间格式yyyyMMdd转yyyy-MM-dd
select from_unixtime(unix_timestamp('20210304','yyyyMMdd'),'yyyy-MM-dd');2021-03-04
时间戳格式转yyyy-MM-dd
select from_unixtime(cast(1614787200 as bigint),'yyyy-MM-dd' )2021-03-04
作弊技巧
select abs(datediff('2016-05-21','2021-03-24'));1768
文章转载自趣说大数据,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




