private Date evalDate(Date d) throws UDFArgumentException {
    date.setTimeInDays(d.toEpochDay());
    if ("MONTH".equals(fmtInput) || "MON".equals(fmtInput) || "MM".equals(fmtInput)) {
      date.setDayOfMonth(1);
      return date;
    } else if ("QUARTER".equals(fmtInput) || "Q".equals(fmtInput)) {
      int month = date.getMonth() - 1;
      int quarter = month / 3;
      int monthToSet = quarter * 3 + 1;
      date.setMonth(monthToSet);
      date.setDayOfMonth(1);
      return date;
    } else if ("YEAR".equals(fmtInput) || "YYYY".equals(fmtInput) || "YY".equals(fmtInput)) {
      date.setMonth(1);
      date.setDayOfMonth(1);
      return date;
    } else {
      return null;
    }
  }
GenericUDFTrunc.

Returns the first day of the month which the date belongs to. The time part of the date will be
ignored.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTrunc.java