1、unix_timestamp()
返回當前時區的unix時間戳
返回型別:bigint
2、from_unixtime(bigint unixtime[,string format])hive>
select unix_timestamp(
)from hive_sum limit1;
1465875016
時間戳轉日期函式
返回型別:string
3、unix_timestamp(string date)hive>
select from_unixtime(unix_timestamp(
),』yyyymmdd』)
from hive_sum limit1;
20160614
返回指定日期格式的的時間戳
返回型別:bigint
注意:如果後面只有date引數,date的形式必須為』yyyy-mm-dd hh:mm:ss』的形式。
4、unix_timestamp(string date,string pattern)hive>
select unix_timestamp(『2016-06
-01』)
from hive_sum limit1;
null
hive>
select unix_timestamp(『2016-06
-0100:00:00』)
from hive_sum limit1;
1464710400
返回指定日期格式的時間戳
返回型別:bigint
5、to_date(string date)hive>
select unix_timestamp(『2016-06
-01』,』yyyymmdd』)
from hive_sum limit1;
1449331200
返回時間欄位中的日期部分
返回型別:string
6、year(string date)hive>
select to_date(『2016-06
-0100:00:00』)
from hive_sum limit1;
2016-06
-01
返回時間欄位中的年
返回型別:int
7、month(string date)hive>
select
year
(『2016-06
-0100:00:00』)
from hive_sum limit1;
2016
返回時間欄位中的月
返回型別:int
6hive>
select
month
(『2016-06
-01』)
from hive_sum limit
1;
8、day(string date)
返回時間欄位中的天
返回型別:int
9、weekofyear(string date)hive>
select
day(『2016-06
-01』)
from hive_sum limit1;
1
返回時間欄位是本年的第多少周
返回型別:int
10、datediff(string enddate,string begindate)hive>
select weekofyear(『2016-06
-01』)
from hive_sum limit1;
22
返回enddate與begindate之間的時間差的天數
返回型別:int
11、date_add(string date,int days)hive>
select datediff(『2016-06
-01』,』2016-05
-01』)
from hive_sum limit1;
31
返回date增加days天後的日期
返回型別:string
12、date_sub(string date,int days)hive>
select date_add(『2016-06
-01』,15)
from hive_sum limit1;
2016-06
-16
返回date減少days天後的日期
返回型別:string
13、add_months(string date,int months)hive>
select date_sub(『2016-06
-01』,15)
from hive_sum limit1;
2016-05
-17
返回date增加months月後的日期
返回型別:string
15.date_format(date,格式)hive>
select add_months(『2016-06
-01』,1)
from hive_sum limit1;
2016-07
-01-`
`1-4、current_date
返回當前日期
返回型別:string``
`sql
select
cust_id
,sum(
case
when update_time between add_months(
current_date,-
3)andcurrent_date
then
1else
0end
)as num_cnt
from src group
by cust_id;
根據格式整理日期
返回型別:string
next_day()select date_format(
'2019-02-10'
,'yyyy-mm');
2019
-02
select next_day(
'2019-02-12'
,'mo'
);
18 . last_day()select date_add(next_day(
'2019-02-12'
,'mo'),
-7);
返回當月的最後一天
19 . months_between(date1, date2)select last_day(
'2019-02-10'
);
返回兩個日期間的月數(
如果date1在日曆中比date2晚,那麼months_between()就返回乙個正數。
如果date1在日曆中比date2早,那麼months_between()就返回乙個負數。
如果date1和date2日期一樣,那麼months_between()就返回乙個0。
)返回型別: int
sql
>
select months_between(to_date(
'2014-3-21'
,'yyyy-mm-dd'
), to_date(
'2014-1-10'
,'yyyy-mm-dd'
)) months
2from dual;
months
----------
2.35483871
sql>
select months_between(to_date(
'2014-1-10'
,'yyyy-mm-dd'
), to_date(
'2014-3-21'
,'yyyy-mm-dd'
)) months
2from dual;
months
----------
-2.3548387
sql>
select months_between(to_date(
'2014-1-10'
,'yyyy-mm-dd'
), to_date(
'2014-1-10'
,'yyyy-mm-dd'
)) months
2from dual;
months
----------
0--2014.3.21和2014.1.10之間,相差2個月加11天,11天按月換算成小數(在oracle裡面,以31天為基數):
sql>
select11/
31from dual;11/
31----------
.35483871
Python之時間函式
import datetime,time defdatetime to long time start datetime datetime格式轉long 毫秒 param time start return return int time.mktime time start.timetuple 10...
通用函式之時間轉換
我們可以把經常用到的函式封裝起來,在抽象類中,然後繼承來使用。時間轉時間戳 param object str return timestamp function totime str,flag false else echo inttime exit return inttime int 時間戳轉時...
Hive 時間函式
to date 日期時間轉日期函式select to date 2015 04 02 13 34 12 輸出 2015 04 02from unixtime 轉化unix時間戳到當前時區的時間格式select from unixtime 1323308943,yyyymmdd 輸出 20111208...