source:Hive 日期时间操作_TriumPhSK的博客-CSDN博客_hive 时间
(1)获取日期
hive> select from_unixtime(unix_timestamp(),"yyyy-MM-dd");
+-------------+
| _c0 |
+-------------+
| 2022-06-24 |
+-------------+
hive> select current_date;
+-------------+
| _c0 |
+-------------+
| 2022-06-24 |
+-------------+
(2)获取带时间的日期
-- 获取当前时间,从第一位开始获取,截取19个字符;
hive> select substr(current_timestamp(),1,19);
+----------------------+
| _c0 |
+----------------------+
| 2022-06-24 14:45:44 |
+----------------------+
-- 获取带有时间戳的时间;
hive> select current_timestamp;
+--------------------------+
| _c0 |
+--------------------------+
| 2022-06-24 14:48:01.597 |
+--------------------------+
(3)获取某一日期前后几天的日期
hive> select add_months(current_timestamp, -6);
+-------------+
| _c0 |
+-------------+
| 2021-12-24 |
+-------------+
hive> select add_months(current_date,-6);
+-------------+
| _c0 |
+-------------+
| 2021-12-24 |
+-------------+
hive> select add_months('2020-12-01',-6);
+-------------+
| _c0 |
+-------------+
| 2020-06-01 |
+-------------+
hive> select date_sub(current_date,6);
+-------------+
| _c0 |
+-------------+
| 2022-06-18 |
+-------------+
hive> select date_add(current_date,6);
+-------------+
| _c0 |
+-------------+
| 2022-06-30 |
+-------------+
(4) 获取当前年月日周
select year(current_timestamp); ----返回日期中的年
select month(current_timestamp); ----返回日期中的月
select day(current_timestamp); ----返回日期中的日
select hour(current_timestamp); ----返回日期中的时
select minute(current_timestamp); ----返回日期中的分
select second(current_timestamp); ----返回日期中的秒
select weekofyear(current_timestamp); ----返回日期在当前的周数
(5)时间戳转日期格式
hive> select from_unixtime(1606989058,'yyyy-MM-dd HH:mm:ss');
+----------------------+
| _c0 |
+----------------------+
| 2020-12-03 17:50:58 |
+----------------------+