package cn.cqc3073.hive.udf; import com.nubia.hive.NewDateUtil; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDF; import java.time.LocalDate; import java.time.temporal.ChronoField; /** * 计算当年的第几周,如201812,表示2018年的第12周 * create function udf.weeekno as 'cn.cqc3073.hive.udf.WeekNo' using jar 'hdfs:///user/hive/udf/hive-jar-with-dependencies.jar'; * * by cqc */ @Description( name = "weekno", value = "_FUNC_(yyyy-MM-dd|current_date) - 计算指定日期属于第几周,如_FUNC_('2018-05-29') => 201822" ) public class WeekNo extends UDF{ public String evaluate(String date) { LocalDate localDate = LocalDate.parse(date, NewDateUtil.FMT_DATE_DASH); return String.format("%d%02d", localDate.getYear(), localDate.get(ChronoField.ALIGNED_WEEK_OF_YEAR)); } }