static DateFormatter forPattern()

in server/src/main/java/org/opensearch/common/time/DateFormatters.java [1961:2160]


    static DateFormatter forPattern(String input) {
        if (Strings.hasLength(input)) {
            input = input.trim();
        }
        if (input == null || input.length() == 0) {
            throw new IllegalArgumentException("No date pattern provided");
        }

        FormatNames formatName = FormatNames.forName(input);
        if (formatName != null && formatName.isCamelCase(input)) {
            String msg = "Camel case format name {} is deprecated and will be removed in a future version. "
                + "Use snake case name {} instead.";
            deprecationLogger.getOrCompute()
                .deprecate(
                    "camelCaseDateFormat_" + formatName.getCamelCaseName(),
                    msg,
                    formatName.getCamelCaseName(),
                    formatName.getSnakeCaseName()
                );
        }

        if (FormatNames.ISO8601.matches(input)) {
            return ISO_8601;
        } else if (FormatNames.BASIC_DATE.matches(input)) {
            return BASIC_DATE;
        } else if (FormatNames.BASIC_DATE_TIME.matches(input)) {
            return BASIC_DATE_TIME;
        } else if (FormatNames.BASIC_DATE_TIME_NO_MILLIS.matches(input)) {
            return BASIC_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.BASIC_ORDINAL_DATE.matches(input)) {
            return BASIC_ORDINAL_DATE;
        } else if (FormatNames.BASIC_ORDINAL_DATE_TIME.matches(input)) {
            return BASIC_ORDINAL_DATE_TIME;
        } else if (FormatNames.BASIC_ORDINAL_DATE_TIME_NO_MILLIS.matches(input)) {
            return BASIC_ORDINAL_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.BASIC_TIME.matches(input)) {
            return BASIC_TIME;
        } else if (FormatNames.BASIC_TIME_NO_MILLIS.matches(input)) {
            return BASIC_TIME_NO_MILLIS;
        } else if (FormatNames.BASIC_T_TIME.matches(input)) {
            return BASIC_T_TIME;
        } else if (FormatNames.BASIC_T_TIME_NO_MILLIS.matches(input)) {
            return BASIC_T_TIME_NO_MILLIS;
        } else if (FormatNames.BASIC_WEEK_DATE.matches(input)) {
            return BASIC_WEEK_DATE;
        } else if (FormatNames.BASIC_WEEK_DATE_TIME.matches(input)) {
            return BASIC_WEEK_DATE_TIME;
        } else if (FormatNames.BASIC_WEEK_DATE_TIME_NO_MILLIS.matches(input)) {
            return BASIC_WEEK_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.DATE.matches(input)) {
            return DATE;
        } else if (FormatNames.DATE_HOUR.matches(input)) {
            return DATE_HOUR;
        } else if (FormatNames.DATE_HOUR_MINUTE.matches(input)) {
            return DATE_HOUR_MINUTE;
        } else if (FormatNames.DATE_HOUR_MINUTE_SECOND.matches(input)) {
            return DATE_HOUR_MINUTE_SECOND;
        } else if (FormatNames.DATE_HOUR_MINUTE_SECOND_FRACTION.matches(input)) {
            return DATE_HOUR_MINUTE_SECOND_FRACTION;
        } else if (FormatNames.DATE_HOUR_MINUTE_SECOND_MILLIS.matches(input)) {
            return DATE_HOUR_MINUTE_SECOND_MILLIS;
        } else if (FormatNames.DATE_OPTIONAL_TIME.matches(input)) {
            return DATE_OPTIONAL_TIME;
        } else if (FormatNames.DATE_TIME.matches(input)) {
            return DATE_TIME;
        } else if (FormatNames.DATE_TIME_NO_MILLIS.matches(input)) {
            return DATE_TIME_NO_MILLIS;
        } else if (FormatNames.HOUR.matches(input)) {
            return HOUR;
        } else if (FormatNames.HOUR_MINUTE.matches(input)) {
            return HOUR_MINUTE;
        } else if (FormatNames.HOUR_MINUTE_SECOND.matches(input)) {
            return HOUR_MINUTE_SECOND;
        } else if (FormatNames.HOUR_MINUTE_SECOND_FRACTION.matches(input)) {
            return HOUR_MINUTE_SECOND_FRACTION;
        } else if (FormatNames.HOUR_MINUTE_SECOND_MILLIS.matches(input)) {
            return HOUR_MINUTE_SECOND_MILLIS;
        } else if (FormatNames.ORDINAL_DATE.matches(input)) {
            return ORDINAL_DATE;
        } else if (FormatNames.ORDINAL_DATE_TIME.matches(input)) {
            return ORDINAL_DATE_TIME;
        } else if (FormatNames.ORDINAL_DATE_TIME_NO_MILLIS.matches(input)) {
            return ORDINAL_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.TIME.matches(input)) {
            return TIME;
        } else if (FormatNames.TIME_NO_MILLIS.matches(input)) {
            return TIME_NO_MILLIS;
        } else if (FormatNames.T_TIME.matches(input)) {
            return T_TIME;
        } else if (FormatNames.T_TIME_NO_MILLIS.matches(input)) {
            return T_TIME_NO_MILLIS;
        } else if (FormatNames.WEEK_DATE.matches(input)) {
            return WEEK_DATE;
        } else if (FormatNames.WEEK_DATE_TIME.matches(input)) {
            return WEEK_DATE_TIME;
        } else if (FormatNames.WEEK_DATE_TIME_NO_MILLIS.matches(input)) {
            return WEEK_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.WEEK_YEAR.matches(input)) {
            deprecationLogger.getOrCompute()
                .deprecate(
                    "week_year_format_name",
                    "Format name \"week_year\" is deprecated and will be removed in a future version. " + "Use \"weekyear\" format instead"
                );
            return WEEK_YEAR;
        } else if (FormatNames.WEEKYEAR.matches(input)) {
            return WEEKYEAR;
        } else if (FormatNames.WEEK_YEAR_WEEK.matches(input)) {
            return WEEKYEAR_WEEK;
        } else if (FormatNames.WEEKYEAR_WEEK_DAY.matches(input)) {
            return WEEKYEAR_WEEK_DAY;
        } else if (FormatNames.YEAR.matches(input)) {
            return YEAR;
        } else if (FormatNames.YEAR_MONTH.matches(input)) {
            return YEAR_MONTH;
        } else if (FormatNames.YEAR_MONTH_DAY.matches(input)) {
            return YEAR_MONTH_DAY;
        } else if (FormatNames.EPOCH_SECOND.matches(input)) {
            return EpochTime.SECONDS_FORMATTER;
        } else if (FormatNames.EPOCH_MILLIS.matches(input)) {
            return EpochTime.MILLIS_FORMATTER;
            // strict date formats here, must be at least 4 digits for year and two for months and two for day
        } else if (FormatNames.STRICT_BASIC_WEEK_DATE.matches(input)) {
            return STRICT_BASIC_WEEK_DATE;
        } else if (FormatNames.STRICT_BASIC_WEEK_DATE_TIME.matches(input)) {
            return STRICT_BASIC_WEEK_DATE_TIME;
        } else if (FormatNames.STRICT_BASIC_WEEK_DATE_TIME_NO_MILLIS.matches(input)) {
            return STRICT_BASIC_WEEK_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.STRICT_DATE.matches(input)) {
            return STRICT_DATE;
        } else if (FormatNames.STRICT_DATE_HOUR.matches(input)) {
            return STRICT_DATE_HOUR;
        } else if (FormatNames.STRICT_DATE_HOUR_MINUTE.matches(input)) {
            return STRICT_DATE_HOUR_MINUTE;
        } else if (FormatNames.STRICT_DATE_HOUR_MINUTE_SECOND.matches(input)) {
            return STRICT_DATE_HOUR_MINUTE_SECOND;
        } else if (FormatNames.STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION.matches(input)) {
            return STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION;
        } else if (FormatNames.STRICT_DATE_HOUR_MINUTE_SECOND_MILLIS.matches(input)) {
            return STRICT_DATE_HOUR_MINUTE_SECOND_MILLIS;
        } else if (FormatNames.STRICT_DATE_OPTIONAL_TIME.matches(input)) {
            return STRICT_DATE_OPTIONAL_TIME;
        } else if (FormatNames.STRICT_DATE_OPTIONAL_TIME_NANOS.matches(input)) {
            return STRICT_DATE_OPTIONAL_TIME_NANOS;
        } else if (FormatNames.STRICT_DATE_TIME.matches(input)) {
            return STRICT_DATE_TIME;
        } else if (FormatNames.STRICT_DATE_TIME_NO_MILLIS.matches(input)) {
            return STRICT_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.STRICT_HOUR.matches(input)) {
            return STRICT_HOUR;
        } else if (FormatNames.STRICT_HOUR_MINUTE.matches(input)) {
            return STRICT_HOUR_MINUTE;
        } else if (FormatNames.STRICT_HOUR_MINUTE_SECOND.matches(input)) {
            return STRICT_HOUR_MINUTE_SECOND;
        } else if (FormatNames.STRICT_HOUR_MINUTE_SECOND_FRACTION.matches(input)) {
            return STRICT_HOUR_MINUTE_SECOND_FRACTION;
        } else if (FormatNames.STRICT_HOUR_MINUTE_SECOND_MILLIS.matches(input)) {
            return STRICT_HOUR_MINUTE_SECOND_MILLIS;
        } else if (FormatNames.STRICT_ORDINAL_DATE.matches(input)) {
            return STRICT_ORDINAL_DATE;
        } else if (FormatNames.STRICT_ORDINAL_DATE_TIME.matches(input)) {
            return STRICT_ORDINAL_DATE_TIME;
        } else if (FormatNames.STRICT_ORDINAL_DATE_TIME_NO_MILLIS.matches(input)) {
            return STRICT_ORDINAL_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.STRICT_TIME.matches(input)) {
            return STRICT_TIME;
        } else if (FormatNames.STRICT_TIME_NO_MILLIS.matches(input)) {
            return STRICT_TIME_NO_MILLIS;
        } else if (FormatNames.STRICT_T_TIME.matches(input)) {
            return STRICT_T_TIME;
        } else if (FormatNames.STRICT_T_TIME_NO_MILLIS.matches(input)) {
            return STRICT_T_TIME_NO_MILLIS;
        } else if (FormatNames.STRICT_WEEK_DATE.matches(input)) {
            return STRICT_WEEK_DATE;
        } else if (FormatNames.STRICT_WEEK_DATE_TIME.matches(input)) {
            return STRICT_WEEK_DATE_TIME;
        } else if (FormatNames.STRICT_WEEK_DATE_TIME_NO_MILLIS.matches(input)) {
            return STRICT_WEEK_DATE_TIME_NO_MILLIS;
        } else if (FormatNames.STRICT_WEEKYEAR.matches(input)) {
            return STRICT_WEEKYEAR;
        } else if (FormatNames.STRICT_WEEKYEAR_WEEK.matches(input)) {
            return STRICT_WEEKYEAR_WEEK;
        } else if (FormatNames.STRICT_WEEKYEAR_WEEK_DAY.matches(input)) {
            return STRICT_WEEKYEAR_WEEK_DAY;
        } else if (FormatNames.STRICT_YEAR.matches(input)) {
            return STRICT_YEAR;
        } else if (FormatNames.STRICT_YEAR_MONTH.matches(input)) {
            return STRICT_YEAR_MONTH;
        } else if (FormatNames.STRICT_YEAR_MONTH_DAY.matches(input)) {
            return STRICT_YEAR_MONTH_DAY;
        } else {
            try {
                return new JavaDateFormatter(
                    input,
                    new DateTimeFormatterBuilder().appendPattern(input).toFormatter(Locale.ROOT).withResolverStyle(ResolverStyle.STRICT)
                );
            } catch (IllegalArgumentException e) {
                throw new IllegalArgumentException("Invalid format: [" + input + "]: " + e.getMessage(), e);
            }
        }
    }