本文为您介绍如何通过evaluate()方法获取指定格式时间。
获取指定时间UDF说明
String UDFGetDate(String date, Long days)
String UDFGetDate(String date, Long months)
- 函数功能:获取指定间隔的时间。返回日期格式为yyyyMMdd、yyyy-MM-dd。
- 参数说明:
- date:表示时间。格式为yyyyMMddHHmmss、yyyyMMdd、yyyy-MM-dd、yyyy-MM-dd HH:mm:ss。
- days:表示间隔的天数。值可以为正或者负。
- months:表示间隔月份。如果值为2,则表示求指定月份向后加两个月的月份;如果值为-2,则表示求指定月份向前减两个月的月份。
UDF使用示例
- 注册函数
UDFGetDate.java测试通过后,将其注册为函数使用。说明 一个UDF从发布到服务端供生产使用,需要经过打包、上传、注册这三个步骤。您可以使用一键发布功能一次性完成这些步骤(Studio会依次执行
mvn clean package
、上传JAR和注册UDF这三个步骤)。详情请参见打包、上传和注册。 - 使用示例
成功注册UDF后,执行以下命令:
- 示例一
运行结果如下。select getDateUDF(20140503,-2);
+-----+ | _c0 | +-----+ | 20140501 | +-----+
- 示例二
运行结果如下。select getDateUDF("20100405",2,10);
+-----+ +-----+ | _c0 | +-----+ | 2010-06-10 | +-----+
- 示例三
运行结果如下。select getDateUDF("20100405",2,'first');
+-----+ | _c0 | +-----+ | 2010-06-01 | +-----+ ---+
- 示例四
运行结果如下。select getDateUDF("20100405",-2,"10");
+-----+ | _c0 | +-----+ | 2010-02-10 | +-----+---+
- 示例一
UDF代码示例
package com.aliyun.odps.examples.udf; // package名称,可以根据您的情况定义。
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.aliyun.odps.io.Text;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.udf.annotation.UdfProperty;
@UdfProperty(isDeterministic=true)
public class UDFGetDate extends UDF{
public String evaluate(String riqi, Long days) {
try {
// 判断日期。
String year = riqi.substring(0, 2);
if (!"18".equals(year) && !"19".equals(year) && !"20".equals(year)
&& !"21".equals(year)) {
return "err";
}
String date = "";
if (riqi.endsWith(".0")) {
riqi = riqi.substring(0, riqi.length() - 2);
}
if (riqi.indexOf("-") >= 0) {
riqi = riqi.substring(0, 10);
riqi = riqi.replaceAll("-", "");
} else {
riqi = riqi.substring(0, 8);
}
try {
// 对指定日期进行处理:(+/-间隔天数)。
date = getDay(riqi, days.intValue());
} catch (Exception e) {
return "err";
}
return date;
} catch (Exception e) {
return "err";
}
}
public String evaluate(String riqi, Long n, String flag) {
try {
// 判断日期。
String year = riqi.substring(0, 2);
if (!"18".equals(year) && !"19".equals(year) && !"20".equals(year)
&& !"21".equals(year)) {
return "err";
}
} catch (Exception e) {
return "err";
}
try {
String date = "";
if (riqi.endsWith(".0")) {
riqi = riqi.substring(0, riqi.length() - 2);
}
if (riqi.indexOf("-") >= 0) {
riqi = riqi.substring(0, 10);
riqi = riqi.replaceAll("-", "");
} else {
riqi = riqi.substring(0, 8);
}
try {
// 如果flag为first,则日期为获取月份和当月的第一天。
if ("first".equals(flag)) {
date = getMonthStartDate(riqi, n.intValue());
} else if ("last".equals(flag)) {
date = getMonthEndDate(riqi, n.intValue());
} else {
// 如果flag为last,则日期为获取月份和当月的最后一天。
date = getMonthDate(riqi, n.intValue(), flag);
}
} catch (Exception e) {
return "err";
}
return date;
} catch (Exception e) {
return "err";
}
}
public String evaluate(Long n) {
if (null == n)
return "err";
return getNmonthAgo(n.intValue());
}
private String getMonthDate(String riqi, int n, String flag) {
Calendar ca = Calendar.getInstance();
Date dtBegin = new Date();
try {
dtBegin = new SimpleDateFormat("yyyyMMdd").parse(riqi);
} catch (ParseException e1) {
e1.printStackTrace();
}
int day = Integer.parseInt(flag);
ca.setTime(dtBegin);
ca.add(Calendar.MONTH, n);
ca.set(Calendar.HOUR_OF_DAY, 0);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
ca.set(Calendar.DAY_OF_MONTH, day);
Date firstDate = ca.getTime();
return ymdFormat(firstDate);
}
private String getNmonthAgo(int n) {
Calendar ca = Calendar.getInstance();
Date dtBegin = new Date();
ca.setTime(dtBegin);
ca.add(Calendar.MONTH, n);
Date firstDate = ca.getTime();
return ymdhmsFormat(firstDate);
}
// 对指定日期进行处理:(+/-间隔天数)。
private String getDay(String strBeginDate, int n) {
Date dtBegin = new Date();
try {
dtBegin = new SimpleDateFormat("yyyyMMdd").parse(strBeginDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
Calendar cld = Calendar.getInstance();
cld.setTime(dtBegin);
int day = cld.get(Calendar.DAY_OF_YEAR);
String strYear = strBeginDate.substring(0, 4);
String strInputPath = "";
String strDate = "";
try {
int nDays = 1;
cld.setTime(dtBegin);
cld.set(Calendar.DAY_OF_YEAR, day + n);
Date dt = cld.getTime();
strDate = new SimpleDateFormat("yyyyMMdd").format(dt);
strYear = strDate.substring(0, 4);
strInputPath = strDate;
} catch (NumberFormatException e) {
e.printStackTrace();
}
return strInputPath;
}
// 对指定日期进行设置flag处理。
private String getMonthStartDate(String riqi, int n) {
Calendar ca = Calendar.getInstance();
Date dtBegin = new Date();
try {
dtBegin = new SimpleDateFormat("yyyyMMdd").parse(riqi);
} catch (ParseException e1) {
e1.printStackTrace();
}
ca.setTime(dtBegin);
ca.add(Calendar.MONTH, n);
ca.set(Calendar.HOUR_OF_DAY, 0);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
return ymdFormat(firstDate);
}
private String getMonthEndDate(String riqi, int n) {
Calendar ca = Calendar.getInstance();
Date dtBegin = new Date();
try {
dtBegin = new SimpleDateFormat("yyyyMMdd").parse(riqi);
} catch (ParseException e1) {
e1.printStackTrace();
}
ca.setTime(dtBegin);
ca.add(Calendar.MONTH, n);
ca.set(Calendar.HOUR_OF_DAY, 23);
ca.set(Calendar.MINUTE, 59);
ca.set(Calendar.SECOND, 59);
ca.set(Calendar.DAY_OF_MONTH, 1);
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = new Date(ca.getTime().getTime());
return ymdFormat(lastDate);
}
private String ymdFormat(Date date) {
if (date == null) {
return "";
}
// 获取指定日期格式。
DateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
return ymdFormat.format(date);
}
private String ymdhmsFormat(Date date) {
if (date == null) {
return "";
}
// 获取指定日期格式。
DateFormat ymdFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return ymdFormat.format(date);
}
}