固定日期转换成时间戳
select unix_timestamp('2016-08-16','yyyy-MM-dd') --1471276800
select unix_timestamp('20160816','yyyyMMdd') --1471276800
select unix_timestamp('2016-08-16T10:02:41Z', "yyyy-MM-dd'T'HH:mm:ss'Z'") --1471312961
16/Mar/2017:12:25:01 +0800 转成正常格式(yyyy-MM-dd hh:mm:ss)
select from_unixtime(to_unix_timestamp('16/Mar/2017:12:25:01 +0800', 'dd/MMM/yyy:HH:mm:ss Z'))
时间戳转换程固定日期
select from_unixtime(1471276800,'yyyy-MM-dd') --2016-08-16
select from_unixtime(1471276800,'yyyyMMdd') --20160816
select from_unixtime(1471312961) -- 2016-08-16 10:02:41
select from_unixtime( unix_timestamp('20160816','yyyyMMdd'),'yyyy-MM-dd') --2016-08-16
select date_format('2016-08-16','yyyyMMdd') --20160816
返回日期时间字段中的日期部分
select to_date('2016-08-16 10:03:01') --2016-08-16
取当前时间
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
select from_unixtime(unix_timestamp(),'yyyy-MM-dd')
返回日期中的年
select year('2016-08-16 10:03:01') --2016
返回日期中的月
select month('2016-08-16 10:03:01') --8
返回日期中的日
select day('2016-08-16 10:03:01') --16
返回日期中的时
select hour('2016-08-16 10:03:01') --10
返回日期中的分
select minute('2016-08-16 10:03:01') --3
返回日期中的秒
select second('2016-08-16 10:03:01') --1
返回日期在当前的周数
select weekofyear('2016-08-16 10:03:01') --33
返回结束日期减去开始日期的天数
select datediff('2016-08-16','2016-08-11')
返回开始日期startdate增加days天后的日期
select date_add('2016-08-16',10)
返回开始日期startdate减少days天后的日期
select date_sub('2016-08-16',10)
返回当天三种方式
SELECT CURRENT_DATE;
--2017-06-15
SELECT CURRENT_TIMESTAMP;--返回时分秒
--2017-06-15 19:54:44
SELECT from_unixtime(unix_timestamp());
--2017-06-15 19:55:04
返回当前时间戳
Select current_timestamp--2018-06-18 10:37:53.278
返回当月的第一天
select trunc('2016-08-16','MM') --2016-08-01
返回当年的第一天
select trunc('2016-08-16','YEAR') --2016-01-01
原文链接:
https://www.cnblogs.com/allthewayforward/p/11165302.html
在解析埋点数据时会遇到两种不同的
日期
格式:
yyyy
mm
dd
和
yyyy
-
mm
-
dd
,此类型之间的转换主要有两种思路:
第一种方法:from_unixtime+unix_timestamp
--20180905转成2018-09-05
select from_unixtime(unix_timestamp('20180905','
yyyy
mm
dd
'),'
yyyy
-
mm
-
dd
')
from dw....
--20171205转成2017-12-05
select from_unixtime(unix_timestamp('20171205','
yyyy
MM
dd
'),'
yyyy
-
MM
-
dd
') from dual;
--2017-12-05转成20171205
select from_unixtime(unix_times...
regexp_replace(‘
yyyy
-
mm
-
dd
’ ,’-’,’’)
from_unixtime(unix_timestamp(‘2019-08-28’,‘
yyyy
-
mm
-
dd
’),‘
yyyy
mm
dd
’)
concat(substr(‘2018-08-28’,1,4),substr...
Hive
将
yyyy
MM
dd
变成
yyyy
-
MM
-
dd
:
select to_date(from_unixtime(UNIX_TIMESTAMP('20210101','
yyyy
MM
dd
')));
2021-01-01
日期
时间格式大致分成时间戳和
日期
时间格式互转,字符串转化成
日期
时间格式,
日期
格式之间的转化
一、时间戳和
日期
互转
1、unix_timestamp函数,
日期
转时间戳
当函数参数为空时,返回当前时间戳。
select unix_timestamp() ---1571282494
不输入时间格式,默认’
yyyy
-
MM
-
dd
HH:
mm
:ss’格式
select un...
RFC 3339被列为ISO 8601的配置文件 ;
RFC 3339格式:“{年}-{月}-{日}T{时}:{分}:{秒}.{毫秒}{时区}”;
其中的年要用零补齐为4位,月日时分秒则补齐为2位。毫秒部分是可选的。
最后一部分是时区,前面例子中的 Z 其实是零时区 Zulu 的缩写,它也可能是 +08:00 或 -08:00 等;
2017-12-08T00:00:00.00Z
2017-12-08T08:00:00.00+08:00
固定
日期
转换成时间戳select unix_timestamp('2016-08-16','
yyyy
-
MM
-
dd
') --1471276800select unix_timestamp('20160816','
yyyy
MM
dd
') --1471276800select unix_timestamp('2016-08-16T10:02:41Z', "
yyyy
-
MM
-
dd
'T'HH:
mm
:ss'Z'"...
function formatDateTime(inputTime) {
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d
1.使用SimpleDateFormat解析带有微秒,纳秒格式的时间会出现多几分钟的现象,因为SimpleDateFormat只能解析到毫秒,会将微秒等当做毫秒来解析
2.使用LocalDateTime来正确解析,JDK1.8后加入
package javaTEST;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;