文章目录
- 时间函数
- unix_timestamp() ------------------------------------------返回当前时区的unix时间戳
- from_unixtime(unixtime,时间格式) ---------------------时间戳转日期函数
- unix_timestamp(string date) ---------------------------返回指定日期格式的的时间戳
- unix_timestamp(string date,string pattern) ---------------返回指定日期格式的的时间戳
- to_date(string date) -------------------------返回时间字段中的日期部分
- year(string date) -------------------返回时间字段中的年
- month(string date) -----------------------返回时间字段中的月
- day(string date) -------------------返回时间字段中的天
- weekofyear(string date) --------------------------返回时间字段是本年的第多少周
- datediff(string enddate,string begindate) ---------------------------------返回enddate与begindate之间的时间差的天数
- date_add(string date,int days) ------------------------返回date增加days天后的日期
- date_sub(string date,int days) --------------------------------返回date减少days天后的日期
- date_format ----------------------------返回指定日期格式的时间
- next_day() --------------------------------取当前天的下一个周一
时间函数
unix_timestamp() ------------------------------------------返回当前时区的unix时间戳
返回当前时区的unix时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp() from hive_sum limit 1;
1465875016
from_unixtime(unixtime,时间格式) ---------------------时间戳转日期函数
时间戳转日期函数
返回类型:string
hive (tmp)> select from_unixtime(unix_timestamp(),‘yyyyMMdd’) from hive_sum limit 1;
20160614
unix_timestamp(string date) ---------------------------返回指定日期格式的的时间戳
返回指定日期格式的的时间戳
返回类型:bigint
注意:如果后面只有date参数,date的形式必须为’yyyy-MM-dd HH:mm:ss’的形式。
hive (tmp)> select unix_timestamp(‘2016-06-01’) from hive_sum limit 1;
NULL
hive (tmp)> select unix_timestamp(‘2016-06-01 00:00:00’) from hive_sum limit 1;
1464710400
unix_timestamp(string date,string pattern) ---------------返回指定日期格式的的时间戳
返回指定日期格式的时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp(‘2016-06-01’,‘yyyyMMdd’) from hive_sum limit 1;
1449331200
to_date(string date) -------------------------返回时间字段中的日期部分
返回时间字段中的日期部分
返回类型:string
hive (tmp)> select to_date(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016-06-01
year(string date) -------------------返回时间字段中的年
返回时间字段中的年
返回类型:int
hive (tmp)> select year(‘2016-06-01 00:00:00’) from hive_sum limit 1;
2016
month(string date) -----------------------返回时间字段中的月
返回时间字段中的月
返回类型:int
hive (tmp)> select month(‘2016-06-01’) from hive_sum limit 1;
day(string date) -------------------返回时间字段中的天
返回时间字段中的天
返回类型:int
hive (tmp)> select day(‘2016-06-01’) from hive_sum limit 1;
weekofyear(string date) --------------------------返回时间字段是本年的第多少周
返回时间字段是本年的第多少周
返回类型:int
hive (tmp)> select weekofyear(‘2016-06-01’) from hive_sum limit 1;
datediff(string enddate,string begindate) ---------------------------------返回enddate与begindate之间的时间差的天数
返回enddate与begindate之间的时间差的天数
返回类型:int
hive (tmp)> select datediff(‘2016-06-01’,‘2016-05-01’) from hive_sum limit 1;
date_add(string date,int days) ------------------------返回date增加days天后的日期
返回date增加days天后的日期
返回类型:string
hive (tmp)> select date_add(‘2016-06-01’,15) from hive_sum limit 1;
2016-06-16
date_sub(string date,int days) --------------------------------返回date减少days天后的日期
返回date减少days天后的日期
返回类型:string
hive (tmp)> select date_sub(‘2016-06-01’,15) from hive_sum limit 1;
2016-05-17
date_format ----------------------------返回指定日期格式的时间
select date_format(“2023-11-11 04:30:40”,“yyyy-MM-dd HH:mm:ss”)
next_day() --------------------------------取当前天的下一个周一
– 取当前天的下一个周一
select next_day(‘2020-05-22’,‘MO’);
– 2020-05-25
– 取当前周的周一
select date_add(next_day(‘2020-05-22’,‘MO’),-7);