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 MyLocalDateTimeConverter extends AbstractConverter { private String timePattern = "yyyy-MM-dd HH:mm:ss"; private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(timePattern); public MyLocalDateTimeConverter() { super(); } public MyLocalDateTimeConverter(String timePattern) { super(); if (StringUtils.isNotBlank(timePattern) && !this.timePattern.equals(timePattern)) { this.timePattern = timePattern; dateTimeFormatter = DateTimeFormatter.ofPattern(timePattern); } } public MyLocalDateTimeConverter(Object defaultValue) { super(defaultValue); } @Override protected Class getDefaultType() { return LocalDateTime.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 (LocalDateTime.class.equals(type)) { return type.cast(LocalDateTime.parse(value.toString(), dateTimeFormatter)); } throw conversionException(type, value); } }