package com.qunar.hiveudf; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.hadoop.hive.ql.exec.UDF; public class IsSundayOrSaturday extends UDF { public Boolean evaluate(String dateString) { try { //将string转化为date String str = dateString.split(" ")[0]; SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); Date date =sdf.parse(str); //将date转换为calendar Calendar calendar = Calendar.getInstance(); calendar.setTime(date); //获取date的星期 int week = calendar.get(Calendar.DAY_OF_WEEK)-1; if(week == 0 || week == 6) { return true; } else { return false; } } catch (Exception e) { // TODO: handle exception return false; } } }
原文地址:http://blog.csdn.net/magicharvey/article/details/38069897