例如,下面的hive脚本
select
from_unixtime(unix_timestamp('30-Apr-50', 'dd-MMM-yy'), 'yyyy-MM-dd') as date1,
from_unixtime(unix_timestamp('30-Apr-45', 'dd-MMM-yy'), 'yyyy-MM-dd') as date2,
from_unixtime(unix_timestamp('30-Apr-35', 'dd-MMM-yy'), 'yyyy-MM-dd') as date3;
结果如下
date1 date2 date3
1950-04-30 1945-04-30 2035-04-30
将2位数年份转换为4位数年份的unix_timestamp函数背后的逻辑是什么?当转换为2 **时,2位数年度是否有固定阈值?如果有,阈值是多少?是否有一种参数('世纪休息'作为真实情况)我们可以根据某些条件设定世纪?
答案 0 :(得分:1)
年度:...
用于使用缩写的年份模式进行解析(" y"或 " yy"),SimpleDateFormat必须解释缩写的年份相对 到了某个世纪 它通过将日期调整为创建SimpleDateFormat实例之前的80年和之后20年来实现此目的 例如,使用" MM / dd / yy"和1997年1月1日创建的SimpleDateFormat实例,字符串" 01/11/12"将被解释为2012年1月11日,而字符串" 05/04/64"将被解释为1964年5月4日。
hive> select current_date;
2017-03-28
-- 20 years after today
hive> select from_unixtime(unix_timestamp('37-03-28','yy-MM-dd'));
2037-03-28 00:00:00
hive> select from_unixtime(unix_timestamp('37-03-29','yy-MM-dd'));
1937-03-29 00:00:00
-- 80 years before today
hive> select from_unixtime(unix_timestamp('37-03-29','yy-MM-dd'));
1937-03-29 00:00:00
hive> select from_unixtime(unix_timestamp('37-03-28','yy-MM-dd'));
2037-03-28 00:00:00
hive/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFUnixTimeStamp.java
public class GenericUDFUnixTimeStamp extends GenericUDFToUnixTimeStamp {
...
public Object evaluate(DeferredObject[] arguments) throws HiveException {
return (arguments.length == 0) ? currentTimestamp : super.evaluate(arguments);
}
hive/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFToUnixTimeStamp.java
import java.text.SimpleDateFormat;
...
public class GenericUDFToUnixTimeStamp extends GenericUDF {
...
private transient final SimpleDateFormat formatter = new SimpleDateFormat(lasPattern);
...
public Object evaluate(DeferredObject[] arguments) throws HiveException {
...
retValue.set(formatter.parse(textVal).getTime() / 1000);
...
}
}