DayofMonth extends UDF { public Integer evaluate(String dateString) { if (dateString == null) { return null; } if (formatter1 == null) { formatter1 = new SimpleDateFormat(UDFConstants.TIMESTAMP_FORMAT); formatter2 = new SimpleDateFormat(UDFConstants.DATE_FORMAT); calendar = Calendar.getInstance(); formatter1.setLenient(false); formatter2.setLenient(false); } try { Date date = null; if (dateString.length() > UDFConstants.DATE_FORMAT.length()) { date = formatter1.parse(dateString); } else { date = formatter2.parse(dateString); } calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } catch (ParseException e) { LOG.warn(EVALUATE_IGNORE_MESSAGE); return null; } } DayofMonth(Map< String, String > config); Integer evaluate(String dateString);  }
@Test public void testEvaluate() { DayofMonth udf = new DayofMonth(null); assertTrue(udf.evaluate("2013-10-17") == TestUDFCommon.I_17); assertTrue(udf.evaluate("2013-10-16") == TestUDFCommon.I_16); assertTrue(udf.evaluate("2013-02-28") == TestUDFCommon.I_28); assertTrue(udf.evaluate("2013-02-29") == null); assertTrue(udf.evaluate("2013-02-29 09:00:01") == null); }