@Test public void testTimestampToStringTypesWithFormat() throws HiveException {
    ObjectInspector inputOI = PrimitiveObjectInspectorFactory.writableTimestampObjectInspector;
    testCast(STRING, inputOI, timestamp("2009-07-30 00:00:08"),
        "yyyy-MM-dd HH24:mi:ss", "2009-07-30 00:00:08");
    testCast(STRING, inputOI, timestamp("2009-07-30 11:02:00"),
        "MM/dd/yyyy hh24miss", "07/30/2009 110200");
    testCast(STRING, inputOI, timestamp("2009-07-30 01:02:03"), "MM", "07");
    testCast(STRING, inputOI, timestamp("1969-07-30 00:00:00"), "yy", "69");

    testCast(CHAR, 3, inputOI, timestamp("2009-07-30 00:00:08"),
        "yyyy-MM-dd HH24:mi:ss", "200");
    testCast(CHAR, 3, inputOI, timestamp("2009-07-30 11:02:00"),
        "MM/dd/yyyy hh24miss", "07/");
    testCast(CHAR, 3, inputOI, timestamp("2009-07-30 01:02:03"), "MM", "07 ");
    testCast(CHAR, 3, inputOI, timestamp("1969-07-30 00:00:00"), "yy", "69 ");

    testCast(VARCHAR, 3, inputOI, timestamp("2009-07-30 00:00:08"),
        "yyyy-MM-dd HH24:mi:ss", "200");
    testCast(VARCHAR, 3, inputOI, timestamp("2009-07-30 11:02:00"),
        "MM/dd/yyyy hh24miss", "07/");
    testCast(VARCHAR, 3, inputOI, timestamp("2009-07-30 01:02:03"), "MM", "07");
    testCast(VARCHAR, 3, inputOI, timestamp("1969-07-30 00:00:00"), "yy", "69");
  }
Tests CAST (<TIMESTAMP/DATE> AS STRING/CHAR/VARCHAR FORMAT <STRING>) and
CAST (<STRING/CHAR/VARCHAR> AS TIMESTAMP/DATE FORMAT <STRING>).
ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFCastFormat.java