Skip to content

Commit

Permalink
* 兼容LocalDate [Issue alibaba#2908](alibaba#2908)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Feb 8, 2023
1 parent 7fac3bf commit 6ff6fbf
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
</dependency>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
import com.alibaba.excel.converters.integer.IntegerBooleanConverter;
import com.alibaba.excel.converters.integer.IntegerNumberConverter;
import com.alibaba.excel.converters.integer.IntegerStringConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateNumberConverter;
import com.alibaba.excel.converters.localdate.LocalDateDateConverter;
import com.alibaba.excel.converters.localdate.LocalDateNumberConverter;
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.longconverter.LongBooleanConverter;
Expand Down Expand Up @@ -83,6 +86,9 @@ private static void initAllConverter() {
putAllConverter(new DateStringConverter());

putAllConverter(new LocalDateNumberConverter());
putAllConverter(new LocalDateStringConverter());

putAllConverter(new LocalDateTimeNumberConverter());
putAllConverter(new LocalDateTimeStringConverter());

putAllConverter(new DoubleBooleanConverter());
Expand Down Expand Up @@ -121,6 +127,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new ByteNumberConverter());
putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
Expand All @@ -139,6 +146,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter());
putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alibaba.excel.converters.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.WorkBookUtil;

/**
* LocalDate and date converter
*
* @author Jiaju Zhuang
*/
public class LocalDateDateConverter implements Converter<LocalDate> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalDate.class;
}

@Override
public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws Exception {
LocalDateTime localDateTime = value == null ? null : value.atTime(0, 0);
WriteCellData<?> cellData = new WriteCellData<>(localDateTime);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultLocalDateFormat);
return cellData;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.excel.converters.localdatetime;
package com.alibaba.excel.converters.localdate;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

import com.alibaba.excel.converters.Converter;
Expand All @@ -14,15 +15,15 @@
import org.apache.poi.ss.usermodel.DateUtil;

/**
* LocalDateTime and number converter
* LocalDate and number converter
*
* @author Jiaju Zhuang
*/
public class LocalDateNumberConverter implements Converter<LocalDateTime> {
public class LocalDateNumberConverter implements Converter<LocalDate> {

@Override
public Class<?> supportJavaTypeKey() {
return LocalDateTime.class;
return LocalDate.class;
}

@Override
Expand All @@ -31,19 +32,19 @@ public CellDataTypeEnum supportExcelTypeKey() {
}

@Override
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing());
} else {
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
}
}

@Override
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.alibaba.excel.converters.localdate;

import java.text.ParseException;
import java.time.LocalDate;

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;

/**
* LocalDate and string converter
*
* @author Jiaju Zhuang
*/
public class LocalDateStringConverter implements Converter<LocalDate> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalDate.class;
}

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

@Override
public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws ParseException {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.parseLocalDate(cellData.getStringValue(), null, globalConfiguration.getLocale());
} else {
return DateUtils.parseLocalDate(cellData.getStringValue(),
contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale());
}
}

@Override
public WriteCellData<?> convertToExcelData(LocalDate 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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import com.alibaba.excel.util.WorkBookUtil;

/**
* Date and date converter
* LocalDateTime and date converter
*
* @author Jiaju Zhuang
*/
public class LocalDateTimeDateConverter implements Converter<LocalDateTime> {
@Override
public Class<LocalDateTime> supportJavaTypeKey() {
public Class<?> supportJavaTypeKey() {
return LocalDateTime.class;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.alibaba.excel.converters.localdatetime;

import java.math.BigDecimal;
import java.time.LocalDateTime;

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 org.apache.poi.ss.usermodel.DateUtil;

/**
* LocalDateTime and number converter
*
* @author Jiaju Zhuang
*/
public class LocalDateTimeNumberConverter implements Converter<LocalDateTime> {

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

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

@Override
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing());
} else {
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
}
}

@Override
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(
BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing())));
} else {
return new WriteCellData<>(BigDecimal.valueOf(
DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing())));
}
}
}
74 changes: 74 additions & 0 deletions easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
Expand Down Expand Up @@ -59,6 +61,8 @@ public class DateUtils {

public static String defaultDateFormat = DATE_FORMAT_19;

public static String defaultLocalDateFormat = DATE_FORMAT_10;

private DateUtils() {}

/**
Expand Down Expand Up @@ -95,6 +99,25 @@ public static LocalDateTime parseLocalDateTime(String dateString, String dateFor
}
}

/**
* convert string to date
*
* @param dateString
* @param dateFormat
* @param local
* @return
*/
public static LocalDate parseLocalDate(String dateString, String dateFormat, Locale local) {
if (StringUtils.isEmpty(dateFormat)) {
dateFormat = switchDateFormat(dateString);
}
if (local == null) {
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat));
} else {
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat, local));
}
}

/**
* convert string to date
*
Expand Down Expand Up @@ -188,6 +211,38 @@ public static String format(LocalDateTime date, String dateFormat, Locale local)
}
}

/**
* Format date
*
* @param date
* @param dateFormat
* @return
*/
public static String format(LocalDate date, String dateFormat) {
return format(date, dateFormat, null);
}

/**
* Format date
*
* @param date
* @param dateFormat
* @return
*/
public static String format(LocalDate date, String dateFormat, Locale local) {
if (date == null) {
return null;
}
if (StringUtils.isEmpty(dateFormat)) {
dateFormat = defaultLocalDateFormat;
}
if (local == null) {
return date.format(DateTimeFormatter.ofPattern(dateFormat));
} else {
return date.format(DateTimeFormatter.ofPattern(dateFormat, local));
}
}

/**
* Format date
*
Expand Down Expand Up @@ -271,6 +326,25 @@ public static LocalDateTime getLocalDateTime(double date, boolean use1904windowi
return DateUtil.getLocalDateTime(date, use1904windowing, true);
}

/**
* Given an Excel date with either 1900 or 1904 date windowing,
* converts it to a java.time.LocalDate.
*
* Excel Dates and Times are stored without any timezone
* information. If you know (through other means) that your file
* uses a different TimeZone to the system default, you can use
* this version of the getJavaDate() method to handle it.
*
* @param date The Excel date.
* @param use1904windowing true if date uses 1904 windowing,
* or false if using 1900 date windowing.
* @return Java representation of the date, or null if date is not a valid Excel date
*/
public static LocalDate getLocalDate(double date, boolean use1904windowing) {
LocalDateTime localDateTime = getLocalDateTime(date, use1904windowing);
return localDateTime == null ? null : localDateTime.toLocalDate();
}

/**
* Determine if it is a date format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.easyexcel.test.util.TestUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelCommonException;
Expand All @@ -32,12 +34,9 @@ public void invoke(ConverterReadData data, AnalysisContext context) {
public void doAfterAllAnalysed(AnalysisContext context) {
Assert.assertEquals(list.size(), 1);
ConverterReadData data = list.get(0);
try {
Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate());
} catch (ParseException e) {
throw new ExcelCommonException("Test Exception", e);
}
Assert.assertEquals(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null), data.getLocalDateTime());
Assert.assertEquals(TestUtil.TEST_DATE, data.getDate());
Assert.assertEquals(TestUtil.TEST_LOCAL_DATE, data.getLocalDate());
Assert.assertEquals(TestUtil.TEST_LOCAL_DATE_TIME, data.getLocalDateTime());
Assert.assertEquals(data.getBooleanData(), Boolean.TRUE);
Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
Assert.assertEquals(data.getBigInteger().intValue(), BigInteger.ONE.intValue(), 0.0);
Expand Down
Loading

0 comments on commit 6ff6fbf

Please sign in to comment.