package com.ynxbd.wx.config; import org.apache.commons.beanutils.converters.AbstractConverter; import org.apache.commons.lang3.StringUtils; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class MyStringConverter extends AbstractConverter { private String timePattern = "yyyy-MM-dd HH:mm:ss"; private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(timePattern); public MyStringConverter() { super(); } public MyStringConverter(String timePattern) { super(); if (StringUtils.isNotBlank(timePattern) && !this.timePattern.equals(timePattern)) { this.timePattern = timePattern; dateTimeFormatter = DateTimeFormatter.ofPattern(timePattern); } } public MyStringConverter(Object defaultValue) { super(defaultValue); } @Override protected Class getDefaultType() { return String.class; } @Override protected T convertToType(Class type, Object value) throws Throwable { // We have to support Object, too, because this class is sometimes // used for a standard to Object conversion if (String.class.equals(type) || Object.class.equals(type)) { return type.cast(value.toString()); } throw conversionException(type, value); } /** * 自己加上 */ @Override protected String convertToString(Object value) throws Throwable { if (value.getClass().equals(LocalDateTime.class)) { return ((LocalDateTime) value).format(dateTimeFormatter); } return super.convertToString(value); } }