public Object convertValue()

in core/src/main/java/org/apache/struts2/conversion/impl/DateConverter.java [49:154]


    public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName,
            Object value, Class toType) {
        Date result = null;

        if (value instanceof String sa && !sa.isEmpty()) {
            Locale locale = getLocale(context);

            DateFormat df = null;
            if (java.sql.Time.class == toType) {
                df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
            } else if (java.sql.Timestamp.class == toType) {
                Date check;
                SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,
                        DateFormat.MEDIUM, locale);
                SimpleDateFormat fullfmt = new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT, locale);

                SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);

                SimpleDateFormat[] fmts = { fullfmt, dtfmt, dfmt };
                for (SimpleDateFormat fmt : fmts) {
                    try {
                        check = fmt.parse(sa);
                        df = fmt;
                        if (check != null) {
                            break;
                        }
                    } catch (ParseException ignore) {
                    }
                }
            } else if (java.util.Date.class == toType) {
                Date check;
                DateFormat[] dfs = getDateFormats(ActionContext.of(context), locale);

                for (DateFormat df1 : dfs) {
                    try {
                        check = df1.parse(sa);
                        df = df1;
                        if (check != null) {
                            break;
                        }
                    } catch (ParseException ignore) {
                    }
                }
            } else if (java.time.LocalDateTime.class == toType || java.time.LocalDate.class == toType
                    || java.time.LocalTime.class == toType) {
                DateTimeFormatter dtf = null;
                TemporalAccessor check = null;
                DateTimeFormatter[] dfs = getDateTimeFormats(ActionContext.of(context), locale);

                for (DateTimeFormatter df1 : dfs) {
                    try {
                        check = df1.parseBest(sa, LocalDateTime::from, LocalDate::from, LocalTime::from);
                        dtf = df1;
                        break;
                    } catch (DateTimeParseException ignore) {
                    }
                }
                try {
                    if (dtf != null && check instanceof LocalDateTime) {
                        return LocalDateTime.parse(sa, dtf);
                    } else if (dtf != null && check instanceof LocalDate) {
                        return LocalDate.parse(sa, dtf);
                    } else if (dtf != null && check instanceof LocalTime) {
                        return LocalTime.parse(sa, dtf);
                    } else {
                        throw new TypeConversionException("Could not parse date");
                    }
                } catch (DateTimeParseException e) {
                    throw new TypeConversionException("Could not parse date", e);
                }

            } else if (OffsetDateTime.class == toType) {
                DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

                try {
                    return OffsetDateTime.parse(sa, dtf);
                } catch (DateTimeParseException e) {
                    throw new TypeConversionException("Could not parse OffsetDateTime", e);
                }
            }

            // final fallback for dates without time
            if (df == null) {
                df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
            }
            try {
                df.setLenient(false); // let's use strict parsing (XW-341)
                result = df.parse(sa);
                if (!(Date.class == toType)) {
                    try {
                        Constructor<?> constructor = toType.getConstructor(long.class);
                        return constructor.newInstance(result.getTime());
                    } catch (Exception e) {
                        throw new TypeConversionException(
                                "Couldn't create class " + toType + " using default (long) constructor", e);
                    }
                }
            } catch (ParseException e) {
                throw new TypeConversionException("Could not parse date", e);
            }

        } else if (Date.class.isAssignableFrom(value.getClass())) {
            result = (Date) value;
        }
        return result;
    }