excel -> easyexcel java时间转换问题

https://blog.csdn.net/qq_20009015/article/details/85243033

导入日期的有3种方式:
1.excel文档日期列设置为文本类型,用LocalDateTimeConverter.class
2.类中日期变量类型改为Date, 加上@DateTimeFormat
3.excel日期字段是按一定规则生成的数字,自定义转换器将数字转为日期,参考https://blog.csdn.net/qq_20009015/article/details/85243033

LocalDateTime:

public class LocalDateTimeConverter implements Converter<LocalDateTime> {
    @Override
    public Class supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    @Override
    public CellData convertToExcelData(LocalDateTime value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}

如下

    @ExcelProperty(value = "创建时间", converter = LocalDateTimeConverter.class)
    private LocalDateTime createTime;

Date:

package com.alibaba.excel.annotation.format;

@java.lang.annotation.Target({java.lang.annotation.ElementType.FIELD})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Inherited
public @interface DateTimeFormat {
    java.lang.String value() default "";

    boolean use1904windowing() default false;
}

如下

    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    private Date createtime;
原文地址:https://www.cnblogs.com/ukzq/p/14182622.html