package time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.hadoop.hive.ql.exec.UDF;
public final class get_hour_timekey extends UDF
{
SimpleDateFormat df = new SimpleDateFormat(“yyyyMMddHH”);// 设置日期格式20170101
String PVVT_HOUR = “”;
String PVVT_TIMEKEY = “”;
String PVVT_LASTHOUR = “”;
String PVVO_RETURN_VALUE = “”;
Calendar calendar = Calendar.getInstance();
@SuppressWarnings("static-access")
public String evaluate(String timekey) throws ParseException
{
if (timekey == null || timekey.length() == 0)
return null;
timekey = timekey.replace("/", "").replace("\\", "").replace("-", "").replace("_", "").replace(":", "").replace(" ", "");
if (timekey.length() < 12)
return null;
PVVT_HOUR = timekey.substring(0, 10);
PVVT_TIMEKEY = timekey.substring(0, 12);
Calendar calendar = Calendar.getInstance();
calendar.setTime(df.parse(PVVT_HOUR));
calendar.add(calendar.HOUR, -1);
PVVT_LASTHOUR = df.format(calendar.getTime());
if (Double.valueOf(PVVT_TIMEKEY) >= Double.valueOf(PVVT_HOUR + "30"))
PVVO_RETURN_VALUE = PVVT_HOUR.substring(0,8) + " " + PVVT_HOUR.substring(8, 10) + "30";
else
PVVO_RETURN_VALUE = PVVT_LASTHOUR.substring(0,8) + " " + PVVT_LASTHOUR.substring(8, 10) + "30";
return PVVO_RETURN_VALUE;
}
public static void main(String[] args) throws ParseException
{
get_hour_timekey timekey = new get_hour_timekey();
System.out.println(timekey.evaluate(""));
}
}