Hive中日期與時間戳的轉換

2021-08-13 11:43:05 字數 1518 閱讀 6616

什麼是時間戳?

時間戳是指**格林尼治時間**2023年01月01日00時00分00秒(北京時間2023年01月01日時00分00秒)起至現在的總秒數。

注意:不管你在地球上的任何地方,這一時刻的時間戳是相同的。但是!同乙個時間戳在不同的時區會表示不同的時間。比如在集群上通過hive函式轉換的是北京時間,但是在本地python轉換就成了美國的時間。可能是根據配置不同?(猜),但是在資料處理過程中一定要看清楚。(2017.11.21新增)

時間戳=>指定格式的日期

# timestamp是時間戳

# 如果timestamp原先是毫秒,則我們/1000後,會自動轉換成double型別,但是主要from_unixtime要求第乙個引數需要為bigint型別,所以我們需要用cast進行型別轉換。

# 語法from_unixtime(bigint unixtime[, stringformat])

select from_unixtime(timestamp, 'yyyy-mm-dd hh:mm:ss') from test_table

指定格式日期=>時間戳

# 如果不寫第二個引數,預設格式是 yyyy-mm-dd hh:mm:ss

select unix_timestamp('20170101 13:20:30', 'yyyymmdd hh:mm:ss') from test_table

時間=>日期

# 只能接受這種形式

select to_date('2017-01-01 13:20:30') from test_table

日期=>年/月/日/時/分/秒

# year(), month(), day(), hour(), minute(), second()

# week() 返回日期所在的星期數

select

year('2017-01-01 13:20:30') from test_table

日期比較

# 返回結束日期減去開始日期的天數

# 語法:datediff(string enddate, string startdate)

select datediff('2017-01-31', '2017-01-1') from test_table

日期加減

# 語法:date_add(string startdate, int days)

select date_add('2017-01-01', 10) from test_table

select date_sub('2017-01-01', 10) from test_table

hive中日期與時間戳轉換

從1970 01 01 00 00 00 utc到指定時間的秒數。總結 時間戳到日期時間,日期時間到時間戳,日期時間到日期。獲取時間戳 select distinct unix timestamp from test date 時間戳 日期 select distinct from unixtime...

Hive中日期與時間戳轉換

1.時間戳轉成日期 select distinct from unixtime 1441565203,yyyy mm dd hh mm ss from test date 2.日期轉成時間戳 select distinct unix timestamp 20111207 13 01 03 from ...

python 中 日期,時間戳的轉換

一,日期轉換成為時間戳 1,首先需要引入模組,time datetime import time datetime2,把輸入的字元轉換成為陣列 python time strptime 函式根據指定的格式把乙個時間字串解析為時間元組。time.strptime string format tsl 2...