Converter接口

//S:表示接受的类型,T:表示目标类型
public interface Converter<S, T> {
/**
* 实现类型转换的方法
*/
@Nullable
T convert(S source);
}

自定义类型转换器

/**
 * @author songzixian
 * @create 2019-07-23 下午 3:22
 * @description  自定义类型转换器
 */
    public class  StringToDateConverter  implements Converter<String, Date> {
            /*** 用于把 String 类型转成日期类型*/
            @Override
            public Date convert(String source) {
            DateFormat format = null;try {
                if(StringUtils.isEmpty(source)) {
                    throw new NullPointerException("请输入要转换的日期");
                }
                format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format.parse(source);
                return date;} catch (Exception e) {
                throw new RuntimeException("输入日期有误");

            }
        }
    }
Last modification:July 23, 2019
如果觉得这篇技术文章对你有用,请随意赞赏