This topic describes how to use the Evaluate() method to obtain time in a specific format.

UDF used to obtain time in a specific format

String UDFGetDate(String date, Long days)
String UDFGetDate(String date, Long months)
  • Description: used to obtain time at specific intervals. The return value is in the format of yyyyMMdd or yyyy-MM-dd.
  • Parameters:
    • date: the time, which is in the format of yyyyMMddHHmmss, yyyyMMdd, yyyy-MM-dd, or yyyy-MM-dd HH:mm:ss.
    • days: the number of days between two time points. The value can be positive or negative.
    • months: the number of months between two time points. If the value is 2, add two months to the specified month. If the value is -2, subtract two months from the specified month.

UDF example

  • Function registration
    After UDFGetDate.java passes the test, register it as a function.
    Note To publish a UDF to a server for production use, the UDF needs to go through packaging, uploading, and registration. You can use the one-click publish function to complete these steps. MaxCompute Studio allows you to run the mvn clean package command, upload a JAR package, and register the UDF in sequence. For more information, see Package、Upload and Register.
  • Examples
    After the UDF is registered, execute one of the following statements:
    • Example 1
      select getDateUDF(20140503,-2) from dual;
      The result is as follows:
      +-----+
      | _c0 |
      +-----+
      | 20140501 |
      +-----+
    • Example 2
      select getDateUDF("20100405",2,10) from dual;
      The result is as follows:
      +-----+
      +-----+
      | _c0 |
      +-----+
      | 2010-06-10 |
      +-----+
    • Example 3
      select getDateUDF("20100405",2,'first') from dual;
      The result is as follows:
      +-----+
      | _c0 |
      +-----+
      | 2010-06-01 |
      +-----+
      ---+
    • Example 4
      select getDateUDF("20100405",-2,"10") from dual;
      The result is as follows:
      +-----+
      | _c0 |
      +-----+
      | 2010-02-10 |
      +-----+---+

UDF code example

package com.aliyun.odps.examples.udf; // The package name, which can be defined as needed.
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 {
                        // Determine the date.
            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 {
                                // Process a specific date by adding or subtracting the number of days between two time points.
                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 {
                        // Determine the date.
            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 {
                                // If flag is set to first, the first day of that month is returned.
                if ("first".equals(flag)) {
                    date = getMonthStartDate(riqi, n.intValue());
                } else if ("last".equals(flag)) {
                    date = getMonthEndDate(riqi, n.intValue());
                } else {
                                        // If flag is set to last, the last day of that month is returned.
                    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);
    }
        // Process a specific date by adding or subtracting the number of days between two time points.
    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;
    }
        // Apply the flag parameter to a specific date.
    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 "";
        }
                // Obtain the specified date format.
        DateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
        return ymdFormat.format(date);
    }

    private String ymdhmsFormat(Date date) {
        if (date == null) {
            return "";
        }
                // Obtain the specified date format.
        DateFormat ymdFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return ymdFormat.format(date);
    }
}