/** * 计算指定的两个指定的日期之前相差几个自然周 * create function udf.weekdiff as 'cn.cqc3073.hive.udf.WeekDiff' using jar 'hdfs:///user/hive/udf/hive-jar-with-dependencies.jar'; * * by cqc */ @Description( name = "weekdiff", value = "_FUNC_(date1, date2) - Returns the number of weeks between date1 and date2" ) public class WeekDiff extends UDF{ public static int evaluate(String d1, String d2) { long days1 = LocalDate.parse(d1, NewDateUtil.FMT_DATE_DASH).with(DayOfWeek.MONDAY).toEpochDay(); long days2 = LocalDate.parse(d2, NewDateUtil.FMT_DATE_DASH).with(DayOfWeek.MONDAY).toEpochDay(); return (int)(days1 / 7) - (int)(days2 / 7); } }