Hive函数 date_format 使用示例总结

Hive函数 date_format 使用示例总结

Hive函数 date_format 用于将日期或时间戳格式化为指定的输出格式。假设要对时间 2024-03-18 18:18:18.008 进行格式化,以下是一些常见的时间提取格式,这些格式可以在 date_format 函数中使用:

1. yyyy :四位年份,如2024。
SELECT date_format('2024-03-18 18:18:18.008', 'yyyy');
输出:
2024
2. yy :两位年份,如24。
SELECT date_format('2024-03-18 18:18:18.008', 'yy');
输出:
24
3. MM :两位月份,如01表示一月。
SELECT date_format('2024-03-18 18:18:18.008', 'MM');
输出:
03
4. M :一位或两位月份,如1表示一月。
SELECT date_format('2024-03-18 18:18:18.008', 'M');
输出:
3
5. dd :两位日期,如01表示第一天。
SELECT date_format('2024-03-18 18:18:18.008', 'dd');
输出:
18
6. d :一位或两位日期,如1表示第一天。
SELECT date_format('2024-03-18 18:18:18.008', 'd');
输出:
18
7. HH :24小时制的小时,如00表示午夜。
SELECT date_format('2024-03-18 18:18:18.008', 'HH');
输出:
18
8. hh :12小时制的小时,如12表示中午或午夜。
SELECT date_format('2024-03-18 18:18:18.008', 'hh');
输出:
6
9. mm :分钟,如00表示整点。
SELECT date_format('2024-03-18 18:18:18.008', 'mm');
输出:
18
10. ss :秒,如00表示整分。
SELECT date_format('2024-03-18 18:18:18.008', 'ss');
输出:
18
11. S :毫秒,如000表示整毫秒。
SELECT date_format('2024-03-18 18:18:18.008', 'S');
输出:
8
12. a :上午或下午。
SELECT date_format('2024-03-18 18:18:18.008', 'a');
输出:
下午
13. E :星期几的全名,如星期一。
SELECT date_format('2024-03-18 18:18:18.008', 'E');
输出:
星期一
14. w :一年中的第几周。
SELECT date_format('2024-03-18 18:18:18.008', 'w');
输出:
12
15. W :一个月中的第几周。
SELECT date_format('2024-03-18 18:18:18.008', 'W');
输出:
4
16. D :一年中的第几天。
SELECT date_format('2024-03-18 18:18:18.008', 'D');
输出:
78
17. F :一个月中的第三个星期一。
SELECT date_format('2024-03-18 18:18:18.008', 'F');
输出:
3
18. u :ISO-8601标准的星期几,1表示星期一。
SELECT date_format('2024-03-18 18:18:18.008', 'u');
输出:
1

以上SQL示例是根据不同的时间提取格式,对给定的时间 2024-03-18 18:18:18.008 进行格式化处理。


Hive 函数 date_format 部分源码如下:

  @Override
  public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
	...
        try {
          formatter = new SimpleDateFormat(fmtStr);
          formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        } catch (IllegalArgumentException e) {
          // ignore
        }
    ...
   }

  @Override
  public Object evaluate(DeferredObject[] arguments) throws HiveException {
  	...
    String res = formatter.format(date);
    if (res == null) {
      return null;
    }
    output.set(res);
    return output;
  }

通过查询date_format源码得知,在初始化阶段,函数会尝试使用给定的格式字符串( fmtStr )创建一个 SimpleDateFormat 对象,并将其时区设置为UTC。在 evaluate 方法中,函数通过 SimpleDateFormat 对象对输入的日期进行格式化处理,生成格式化后的字符串。如果格式化后的结果为null,则返回null,否则将结果设置到输出对象中并返回。

总之,Hive 函数 date_format 能够支持的时间格式化取决于 SimpleDateFormat 这个类能够支持的类型。我们可以通过查阅 SimpleDateFormat 类的官方文档来获取更详细的信息:https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html

相关推荐

  1. Hive函数 date_format 使用示例总结

    2024-03-20 17:44:02       37 阅读
  2. Hive中窗口函数使用示例

    2024-03-20 17:44:02       45 阅读
  3. Hive函数 EXPLODE 和 POSEXPLODE 使用示例

    2024-03-20 17:44:02       41 阅读
  4. Hive连接函数 concat 和 concat_ws 使用示例

    2024-03-20 17:44:02       36 阅读
  5. Hive字符串匹配函数 LIKE 和 RLIKE 使用示例

    2024-03-20 17:44:02       34 阅读
  6. Hive判空函数 COALESCE 和 NVL 使用示例

    2024-03-20 17:44:02       33 阅读
  7. hive中map相关函数总结

    2024-03-20 17:44:02       72 阅读
  8. hive中array相关函数总结

    2024-03-20 17:44:02       65 阅读
  9. hive中struct相关函数总结

    2024-03-20 17:44:02       71 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 17:44:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 17:44:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 17:44:02       82 阅读
  4. Python语言-面向对象

    2024-03-20 17:44:02       91 阅读

热门阅读

  1. 最大子矩阵和

    2024-03-20 17:44:02       39 阅读
  2. react 和vue区别

    2024-03-20 17:44:02       40 阅读
  3. 基于Python的图形用户界面设计及应用

    2024-03-20 17:44:02       45 阅读
  4. 如何建立数字化招标采购(系统)评价体系?

    2024-03-20 17:44:02       41 阅读
  5. 第八章TypeScript class类

    2024-03-20 17:44:02       36 阅读
  6. 【前端】Vite项目图片动态引入

    2024-03-20 17:44:02       41 阅读
  7. S&P 2023

    2024-03-20 17:44:02       33 阅读
  8. 【C++】struct和class区别

    2024-03-20 17:44:02       41 阅读
  9. git相关指令

    2024-03-20 17:44:02       38 阅读