You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
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> T convertToType(Class<T> 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);
|
|
}
|
|
}
|
|
|
|
|