From 1500f108fcb4efb68d73577a01822780653d479a Mon Sep 17 00:00:00 2001 From: yuhaowin Date: Wed, 26 Jul 2023 22:25:39 +0800 Subject: [PATCH] feat:issue-3298 add LocalTime converter --- .../converters/DefaultConverterLoader.java | 5 ++ .../localtime/LocalTimeStringConverter.java | 52 +++++++++++++++++++ .../com/alibaba/excel/util/DateUtils.java | 52 +++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 easyexcel-core/src/main/java/com/alibaba/excel/converters/localtime/LocalTimeStringConverter.java diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index 8c371932..08c86f97 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java @@ -37,6 +37,7 @@ import com.alibaba.excel.converters.localdate.LocalDateStringConverter; import com.alibaba.excel.converters.localdatetime.LocalDateTimeNumberConverter; import com.alibaba.excel.converters.localdatetime.LocalDateTimeDateConverter; import com.alibaba.excel.converters.localdatetime.LocalDateTimeStringConverter; +import com.alibaba.excel.converters.localtime.LocalTimeStringConverter; import com.alibaba.excel.converters.longconverter.LongBooleanConverter; import com.alibaba.excel.converters.longconverter.LongNumberConverter; import com.alibaba.excel.converters.longconverter.LongStringConverter; @@ -91,6 +92,8 @@ public class DefaultConverterLoader { putAllConverter(new LocalDateTimeNumberConverter()); putAllConverter(new LocalDateTimeStringConverter()); + putAllConverter(new LocalTimeStringConverter()); + putAllConverter(new DoubleBooleanConverter()); putAllConverter(new DoubleNumberConverter()); putAllConverter(new DoubleStringConverter()); @@ -125,6 +128,7 @@ public class DefaultConverterLoader { putWriteConverter(new ByteNumberConverter()); putWriteConverter(new DateDateConverter()); putWriteConverter(new LocalDateTimeDateConverter()); + putWriteConverter(new LocalTimeStringConverter()); putWriteConverter(new LocalDateDateConverter()); putWriteConverter(new DoubleNumberConverter()); putWriteConverter(new FloatNumberConverter()); @@ -146,6 +150,7 @@ public class DefaultConverterLoader { putWriteStringConverter(new DateStringConverter()); putWriteStringConverter(new LocalDateStringConverter()); putWriteStringConverter(new LocalDateTimeStringConverter()); + putWriteStringConverter(new LocalTimeStringConverter()); putWriteStringConverter(new DoubleStringConverter()); putWriteStringConverter(new FloatStringConverter()); putWriteStringConverter(new IntegerStringConverter()); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localtime/LocalTimeStringConverter.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/localtime/LocalTimeStringConverter.java new file mode 100644 index 00000000..08363e6f --- /dev/null +++ b/easyexcel-core/src/main/java/com/alibaba/excel/converters/localtime/LocalTimeStringConverter.java @@ -0,0 +1,52 @@ +package com.alibaba.excel.converters.localtime; + +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 com.alibaba.excel.util.DateUtils; + +import java.text.ParseException; +import java.time.LocalTime; + +/** + * LocalTime and string converter + * + * @author yuhaowin + */ +public class LocalTimeStringConverter implements Converter { + @Override + public Class supportJavaTypeKey() { + return LocalTime.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public LocalTime convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) throws ParseException { + if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { + return DateUtils.parseLocalTime(cellData.getStringValue(), null, globalConfiguration.getLocale()); + } else { + return DateUtils.parseLocalTime(cellData.getStringValue(), + contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale()); + } + } + + @Override + public WriteCellData convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) { + if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { + return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale())); + } else { + return new WriteCellData<>( + DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat(), + globalConfiguration.getLocale())); + } + } +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java index cab7e4d0..73af79d3 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java @@ -58,6 +58,7 @@ public class DateUtils { public static final String DATE_FORMAT_19 = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_FORMAT_19_FORWARD_SLASH = "yyyy/MM/dd HH:mm:ss"; private static final String MINUS = "-"; + private static final String DEFAULT_LOCAL_TIME_FORMAT = "HH:mm:ss"; public static String defaultDateFormat = DATE_FORMAT_19; @@ -118,6 +119,25 @@ public class DateUtils { } } + /** + * convert string to date + * + * @param dateString + * @param dateFormat + * @param local + * @return + */ + public static LocalTime parseLocalTime(String dateString, String dateFormat, Locale local) { + if (StringUtils.isEmpty(dateFormat)) { + dateFormat = DEFAULT_LOCAL_TIME_FORMAT; + } + if (local == null) { + return LocalTime.parse(dateString, DateTimeFormatter.ofPattern(dateFormat)); + } else { + return LocalTime.parse(dateString, DateTimeFormatter.ofPattern(dateFormat, local)); + } + } + /** * convert string to date * @@ -254,6 +274,38 @@ public class DateUtils { return format(date, dateFormat, null); } + /** + * Format date + * + * @param date + * @param dateFormat + * @return + */ + public static String format(LocalTime date, String dateFormat) { + return format(date, dateFormat, null); + } + + /** + * Format date + * + * @param date + * @param dateFormat + * @return + */ + public static String format(LocalTime date, String dateFormat, Locale local) { + if (date == null) { + return null; + } + if (StringUtils.isEmpty(dateFormat)) { + dateFormat = DEFAULT_LOCAL_TIME_FORMAT; + } + if (local == null) { + return date.format(DateTimeFormatter.ofPattern(dateFormat)); + } else { + return date.format(DateTimeFormatter.ofPattern(dateFormat, local)); + } + } + /** * Format date *