1.date_format 释义:格式化日期 用法:date_format(date,格式) 例如: 将日期格式化为:2020-05-01和2020-05 hive (default)> select date_format(‘2020-05-01 12:00:00’,‘yyyy-MM-dd’); _c0 2020-05-01 hive (default)> select date_format(‘2020-05-01 12:00:00’,‘yyyy-MM’); _c0 2020-05
2.date_add 释义:日期加法函数,数字为正,则加多少天,若数字为负数,则为减多少天; 用法:date_add(date,number); 例如: 将日期增加或减少4天; hive (default)> select date_add(‘2019-05-09’,4); _c0 2019-05-13 Time taken: 0.034 seconds, Fetched: 1 row(s) hive (default)> select date_add(‘2019-05-09’,-4); _c0 2019-05-05 Time taken: 0.049 seconds, Fetched: 1 row(s)
3.date_sub 释义:日期减法函数,数字为正,则减多少天,若数字为负数,则为加多少天 用法:date_sub(date,number) 例如: 将日期增加或减少4天 hive (default)> select date_sub(‘2019-05-09’,4); _c0 2019-05-05 Time taken: 0.04 seconds, Fetched: 1 row(s) hive (default)> select date_sub(‘2019-05-09’,-4); _c0 2019-05-13 Time taken: 0.058 seconds, Fetched: 1 row(s) 4.next_day 释义:取该日期的下一个周几的日期 用法: next_day(date,dayofweek) 星期一到星期日的英文(Monday,Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday) 例如: 取5.1后的下一个周五 hive (default)> select next_day(‘2020-05-01’,“Fri”); _c0 2020-05-08 5.last_day 释义:取当月的最后一天日期 用法:last_day(date) 例如: 取5月的最后一天日期 hive (default)> select last_day(‘2020-05-01’); _c0 2020-05-31 hive (default)> select last_day(‘2020-04-01’); _c0 2020-04-30 6.datediff 释义:日期比较函数,第一个日期减去第二个时期数字,为正,则前者大于后者,为负,则前者小于后者; 用法:datediff(date1,date2) 例如: 比较5月1日和5月5日的大小 hive (default)> select datediff(‘2020-05-01’,‘2020-05-05’); _c0 -4 7.dayofmonth 释义:查询该日期在本月的第多少天 用法:dayofmonth(date) 例如: 5月6号在五月是第多少天 hive (default)> select dayofmonth(‘2020-05-06’); _c0 6 8.current_date 释义:获取当前日期 hive (default)> select current_date; _c0 2020-05-14 9.current_timestamp 释义:获取当前时间 hive (default)> select current_timestamp; _c0 2020-05-14 10:26:57.613 10.add_months 此函数在hive1.1.0版本后 释义:日期加一个月 用法:add_months(date,number)、add_months(string start_date, int num_months, output_date_format) 例如: 2020-05-03加一个月 实例一: > select add_months(‘2019-02-28’, 1); ±------------±-+ | _c0 | ±------------±-+ | 2019-03-31 | ±------------±-+ 实例二: > select add_months(‘2019-02-24 21:15:16’, 2, ‘YYYY-MM-dd HH:mm:ss’); ±---------------------±-+ | _c0 | ±---------------------±-+ | 2019-04-24 21:15:16 | ±---------------------±-+ 11.year 释义:获取时间的年份 用法:year(date) 例如: 当前日期所在的年份 hive (default)> select year(‘2020-05-14 12:00:00’); _c0 2020 12.month 释义:获取时间的月份 用法:month(date) 例如: 当前日期所在的月份 hive (default)> select month(‘2020-05-14 12:00:00’); _c0 5 13.day 释义:获取时间的天 用法:day(date) 例如: 获取日期中的天 hive (default)> select day(‘2020-05-14 12:00:00’); _c0 14 14.hour 释义:获取时间的小时 用法:hour(time) 若时间为空,则取出值为null 例如: 获取时间中的小时 hive (default)> select hour(‘2020-05-14 12:00:00’); _c0 12 15.minute 释义:获取时间的分钟 用法:minute(time) 若时间为空,则取出值为null 例如: 获取时间中的分钟 hive (default)> select minute(‘2020-05-14 12:04:50’); _c0 4 16.second 释义:获取时间的秒 用法:second(time) 若时间为空,则取出值为null 例如: 获取时间中的秒 hive (default)> select second(‘2020-05-14 12:04:53’); _c0 53 17.weekofyear 释义:日期所在年份的第多少周 用法:weekofyear(date) 例如: 当前日期是所在年份的第多少周 hive (default)> select weekofyear(current_date); _c0 20 18.to_date 释义:转日期函数,默认转为yyyy-MM-dd格式 用法:to_date(time) 例如: 当前时间转为日期格式 hive (default)> select to_date(current_timestamp); _c0 2020-05-14
常用日期需求 1.取当月第1天 1.1 --先获取当前日期在该月份的第n天,然后当前日期减去第(n-1)天,则为结果 hive (default)> select date_sub(‘2020-05-14’,dayofmonth(‘2020-05-14’)-1); _c0 2020-05-01 1.2 – trunc(string date, string format) > select trunc(‘2019-02-24’, ‘MM’); ±------------±-+ | _c0 | ±------------±-+ | 2019-02-01 | ±------------±-+ 2.取当月第6天 –先获取该日期在该月的第n天,然后当前日期减去第(n-1)天,再增加(m-1)天,则为结果 hive (default)> select date_add(date_sub(‘2020-05-14’,dayofmonth(‘2020-05-14’)-1),5); _c0 2020-05-06 3.查询下一个月的第一天 3.1 --先获取最后一天,然后日期+1,则为下一月第一天 hive (default)> select date_add(last_day(‘2020-05-14’),1); _c0 2020-06-01 3.2 --先获取今天是当月第几天,算出当月第一天,然后加一个月,则为下月第一天 hive (default)> select add_months(date_sub(‘2020-05-14’,dayofmonth(‘2020-05-14’)-1),1); _c0 2020-06-01 4.获取当前日期的所在月份 –日期格式化方式 hive (default)> select date_format(‘2020-05-14’,‘MM’); _c0 05 –直接month获取方式 hive (default)> select month(‘2020-05-14’); _c0 5 5.获取本周一的日期 –先获取下周一的日期,然后减去7天,则为本周一日期 hive (default)> select date_add(next_day(current_date,“MO”),-7); _c0 2020-04-27 6.给出一个日期计算该日期为周几 –先找一个周一的日期,比如我们找的2020-05-04,然后和当前日期(2020-05-14)做比较,就会得出相差的天数,再用pmod获取正余数的函数来获取最后的余数(0-6分别代表周日~周六),本实例做+1操作,直接得出周几的结果; hive (default)> select pmod(datediff(‘2020-05-14’,‘2020-05-04’) + 1,7); _c0 4 7.获取指定日期年份的第一天 , trunc(string date, string format) > select trunc(‘2019-02-24’, ‘YYYY’); ±------------±-+ | _c0 | ±------------±-+ | 2019-01-01 | ±------------±-+
8.指定日期下周的指定周几,自带的函数:next_day(string start_date, string day_of_week) – hive1.2.0版本后才支持此函数
> select next_day('2019-02-24', 'TU'); +-------------+--+ | _c0 | +-------------+--+ | 2019-02-26 | +-------------+--+ > select next_day('2019-02-24', 'MONDAY'); +-------------+--+ | _c0 | +-------------+--+ | 2019-02-25 | +-------------+--+