select day -- 時間 ,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 all select '2018-02-02 02:03:04' as day union all select '2018-03-02 03:05:07' as day union all select '2018-04-02 04:07:10' as day union all select '2018-05-02 05:09:13' as day union all select '2018-06-02 06:11:16' as day union all select '2018-07-02 07:13:19' as day union all select '2018-08-02 08:15:22' as day union all select '2018-09-02 09:17:25' as day union all select '2018-10-02 10:19:28' as day union all select '2018-11-02 11:21:31' as day union all select '2018-12-02 12:23:34' as day ) t1 ;
獲取當前時間截:
1 select unix_timestamp() ; 2 +-------------+--+ 3 | _c0 | 4 +-------------+--+ 5 | 1521684090 | 6 +-------------+--+
獲取當前時間1:
1 select current_timestamp; 2 +--------------------------+--+ 3 | _c0 | 4 +--------------------------+--+ 5 | 2018-03-22 10:04:02.568 | 6 +--------------------------+--+
獲取當前時間2:
1 SELECT from_unixtime(unix_timestamp()); 2 +----------------------+--+ 3 | _c0 | 4 +----------------------+--+ 5 | 2018-03-22 10:04:38 | 6 +----------------------+--+
獲取當前日期:
1 SELECT CURRENT_DATE; 2 +-------------+--+ 3 | _c0 | 4 +-------------+--+ 5 | 2018-03-22 | 6 +-------------+--+
日期差值:datediff(結束日期,開始日期),返回結束日期減去開始日期的天數。
1 select datediff(CURRENT_DATE,'2017-01-01') as datediff; 2 +-----------+--+ 3 | datediff | 4 +-----------+--+ 5 | 445 | 6 +-----------+--+
日期加減:date_add(時間,增加天數),返回值為時間天+增加天的日期;date_sub(時間,減少天數),返回日期減少天后的日期。
1 select date_add(current_date,365) as dateadd; 2 +-------------+--+ 3 | dateadd | 4 +-------------+--+ 5 | 2019-03-22 | 6 +-------------+--+
時間差:兩個日期之間的小時差
1 select (hour('2018-02-27 10:00:00')-hour('2018-02-25 12:00:00')+(datediff('2018-02-27 10:00:00','2018-02-25 12:00:00'))*24) as hour_subValue; 2 +----------------+--+ 3 | hour_subValue | 4 +----------------+--+ 5 | 46 | 6 +----------------+--+
獲取年、月、日、小時、分鍾、秒、當年第幾周
1 select 2 year('2018-02-27 10:00:00') as year 3 ,month('2018-02-27 10:00:00') as month 4 ,day('2018-02-27 10:00:00') as day 5 ,hour('2018-02-27 10:00:00') as hour 6 ,minute('2018-02-27 10:00:00') as minute 7 ,second('2018-02-27 10:00:00') as second 8 ,weekofyear('2018-02-27 10:00:00') as weekofyear 9 ; 10 +-------+--------+------+-------+---------+---------+-------------+--+ 11 | year | month | day | hour | minute | second | weekofyear | 12 +-------+--------+------+-------+---------+---------+-------------+--+ 13 | 2018 | 2 | 27 | 10 | 0 | 0 | 9 | 14 +-------+--------+------+-------+---------+---------+-------------+--+
轉成日期:
1 select to_date('2018-02-27 10:03:01') ; 2 +-------------+--+ 3 | _c0 | 4 +-------------+--+ 5 | 2018-02-27 | 6 +-------------+--+
當月最后一天:
1 select last_day('2018-02-27 10:03:01'); 2 +-------------+--+ 3 | _c0 | 4 +-------------+--+ 5 | 2018-02-28 | 6 +-------------+--+
當月第一天:
1 select trunc(current_date,'MM') as day; 2 +-------------+--+ 3 | day | 4 +-------------+--+ 5 | 2018-03-01 | 6 +-------------+--+
當年第一天:
1 select trunc(current_date,'YY') as day; 2 +-------------+--+ 3 | day | 4 +-------------+--+ 5 | 2018-01-01 | 6 +-------------+--+
next_day,返回當前時間的下一個星期幾所對應的日期
1 select next_day('2018-02-27 10:03:01', 'TU'); 2 +-------------+--+ 3 | _c0 | 4 +-------------+--+ 5 | 2018-03-06 | 6 +-------------+--+
-- hive中怎么獲取兩個日期相減后的小時(精確到兩位小數點),而且這兩個日期有可能會出現一個日期有時分秒,一個日期沒有時分秒的情況 select t3.day1 ,t3.day2 ,t3.day -- 日期 ,t3.hour -- 小時 ,t3.min -- 分鍾 ,t3.day + t3.hour as hour_diff_1 ,t3.day + t3.hour + t3.min as hour_diff_2 ,round((cast(cast(t3.day1 as timestamp) as bigint) - cast(cast(t3.day2 as timestamp) as bigint)) / 3600,2) as hour_diff_3 -- 最優 ,(datediff(t3.day1,t3.day2) * 24) + (nvl(hour(t3.day1),0) - nvl(hour(t3.day2),0)) + round((nvl(minute(t3.day1),0) - nvl(minute(t3.day2),0)) / 60,2) as hour_diff_4 from ( select t2.day1 ,t2.day2 ,(datediff(t2.day1,t2.day2) * 24) as day -- 日期 ,(hour(t2.day1) - hour(t2.day2)) as hour -- 小時 ,round((minute(t2.day1) - minute(t2.day2)) / 60,2) as min -- 分鍾 from ( select cast(t1.day1 as timestamp) as day1 ,cast(t1.day2 as timestamp) as day2 from ( select '2018-01-03 02:30:00' as day1, '2018-01-02 23:00:00' as day2 union all select '2018-06-02 08:15:22' as day1, '2018-06-02 06:11:16' as day2 union all select '2018-07-04' as day1, '2018-07-02 01:01:01' as day2 ) t1 ) t2 ) t3 ;
+------------------------+------------------------+------+-------+--------+--------------+--------------+--------------+--------------+--+ | day1 | day2 | day | hour | min | hour_diff_1 | hour_diff_2 | hour_diff_3 | hour_diff_4 | +------------------------+------------------------+------+-------+--------+--------------+--------------+--------------+--------------+--+ | 2018-07-04 00:00:00.0 | 2018-07-02 01:01:01.0 | 48 | -1 | -0.02 | 47 | 46.98 | 46.98 | 46.98 | | 2018-01-03 02:30:00.0 | 2018-01-02 23:00:00.0 | 24 | -21 | 0.5 | 3 | 3.5 | 3.5 | 3.5 | | 2018-06-02 08:15:22.0 | 2018-06-02 06:11:16.0 | 0 | 2 | 0.07 | 2 | 2.07 | 2.07 | 2.07 | +------------------------+------------------------+------+-------+--------+--------------+--------------+--------------+--------------+--+
### 當周第一天,最后一天 date -d "2018-10-24 $(($(date -d 2018-10-24 +%u)-1)) days ago" +%Y-%m-%d date -d "2018-10-24 $((7-$(date -d 2018-10-24 +%u))) days" +%Y-%m-%d