io.github.bootystar.helper.easyexcel.converter.LocalTimeConverter Maven / Gradle / Ivy
The newest version!
package io.github.bootystar.helper.easyexcel.converter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* Excel时间转换器
*
* @author booty
*/
public class LocalTimeConverter implements Converter {
private final DateTimeFormatter formatter =DateTimeFormatter.ofPattern("HH:mm:ss");
@Override
public Class supportJavaTypeKey() {
return LocalTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalTime convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String cellValue = cellData.getStringValue();
if (cellValue == null || cellValue.isEmpty()) {
return null;
}
return LocalTime.parse(cellValue, formatter);
}
@Override
public WriteCellData convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (value == null) {
return new WriteCellData<>("");
}
return new WriteCellData<>(value.format(formatter));
}
}