public static DateTimeFormatter createDateTimeFormatter(String format, Mode mode)
    {
        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
        boolean formatContainsHourOfAMPM = false;
        for (Token token : tokenize(format)) {
            switch (token.getType()) {
                case DateFormat.TEXT:
                    builder.appendLiteral(token.getText());
                    break;
                case DateFormat.DD:
                    builder.appendValue(DAY_OF_MONTH, mode.getMinTwoPositionFieldWidth(), 2, NOT_NEGATIVE);
                    break;
                case DateFormat.HH24:
                    builder.appendValue(HOUR_OF_DAY, mode.getMinTwoPositionFieldWidth(), 2, NOT_NEGATIVE);
                    break;
                case DateFormat.HH:
                    builder.appendValue(HOUR_OF_AMPM, mode.getMinTwoPositionFieldWidth(), 2, NOT_NEGATIVE);
                    formatContainsHourOfAMPM = true;
                    break;
                case DateFormat.MI:
                    builder.appendValue(MINUTE_OF_HOUR, mode.getMinTwoPositionFieldWidth(), 2, NOT_NEGATIVE);
                    break;
                case DateFormat.MM:
                    builder.appendValue(MONTH_OF_YEAR, mode.getMinTwoPositionFieldWidth(), 2, NOT_NEGATIVE);
                    break;
                case DateFormat.SS:
                    builder.appendValue(SECOND_OF_MINUTE, mode.getMinTwoPositionFieldWidth(), 2, NOT_NEGATIVE);
                    break;
                case DateFormat.YY:
                    builder.appendValueReduced(YEAR, 2, 2, 2000);
                    break;
                case DateFormat.YYYY:
                    builder.appendValue(YEAR, 4);
                    break;
                case DateFormat.UNRECOGNIZED:
                default:
                    throw new PrestoException(
                            StandardErrorCode.INVALID_FUNCTION_ARGUMENT,
                            String.format("Failed to tokenize string [%s] at offset [%d]", token.getText(), token.getCharPositionInLine()));
            }
        }
        try {
            // Append default values(0) for time fields(HH24, HH, MI, SS) because JSR-310 does not accept bare Date value as DateTime

            if (formatContainsHourOfAMPM) {
                // At the moment format does not allow to include AM/PM token, thus it was never possible to specify PM hours using 'HH' token in format
                // Keep existing behaviour by defaulting to 0(AM) for AMPM_OF_DAY if format string contains 'HH'
                builder.parseDefaulting(HOUR_OF_AMPM, 0)
                        .parseDefaulting(AMPM_OF_DAY, 0);
            }
            else {
                builder.parseDefaulting(HOUR_OF_DAY, 0);
            }

            return builder.parseDefaulting(MINUTE_OF_HOUR, 0)
                    .parseDefaulting(SECOND_OF_MINUTE, 0)
                    .toFormatter();
        }
        catch (UnsupportedOperationException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
        }
    }
@Test(expectedExceptions = PrestoException.class)
    public void testParserInvalidTokenCreate1()
    {
        DateFormatParser.createDateTimeFormatter("ala", PARSER);
    }