问题描述
我正在寻找提供星期几的解决方法或配置单元日期函数,
I'm looking for a workaround or hive date functions that gives day of the week ,
Sunday - 1
Monday - 2
Tuesday - 3
Wednesday - 4
Thursday - 5
Friday - 6
Saturday - 7
详细要求:我正在寻找一个以日期字符串 (YYYYMMDD) 作为输入并按照上表输出星期几的函数.
Requirement in detail : I'm looking for a function that takes date string (YYYYMMDD) as input and outputs the day of the week as per the above table.
推荐答案
正如我所说,您需要编写一个 UDF,它将接受一个字符串作为参数并返回一个字符串.在 UDF 中,您需要执行以下步骤:
As I said you need to write a UDF which will accept a string as parameter and return a string.Inside the UDF you need to do these steps:
1.) 使用 SimpleDateFormat(YYYYMMDD)
2.) 使用以下代码获取星期几:
2.) Use the Below code to get the day of week:
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
3.) 在 case 语句中使用这个 dayOfWeek 值来获取您的工作日字符串并返回该字符串.
3.) Use this dayOfWeek value in a case statement to get your weekday String and return that string.
希望这会有所帮助...!!!
Hope this helps...!!!
这篇关于Hive日期功能实现星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!