Browse Source

!160 【轻量级pr】poi ExcelUtil 增加对java8 日期的支持

Merge pull request !160 from dazer007/excelUtil_add_java8_date
tags/v3.5.0
若依 3 years ago committed by Gitee
parent
commit
e059ed9c7f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
  1. 20
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java
  2. 31
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

20
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java

@ -3,6 +3,7 @@ package com.ruoyi.common.core.utils; @@ -3,6 +3,7 @@ package com.ruoyi.common.core.utils;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
@ -152,4 +153,23 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils @@ -152,4 +153,23 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
/**
* 增加 LocalDateTime ==> Date
*/
public static Date toDate(LocalDateTime temporalAccessor)
{
ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
/**
* 增加 LocalDate ==> Date
*/
public static Date toDate(LocalDate temporalAccessor)
{
LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
}

31
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@ -6,6 +6,8 @@ import java.lang.reflect.Field; @@ -6,6 +6,8 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@ -282,7 +284,7 @@ public class ExcelUtil<T> @@ -282,7 +284,7 @@ public class ExcelUtil<T>
String dateFormat = field.getAnnotation(Excel.class).dateFormat();
if (StringUtils.isNotEmpty(dateFormat))
{
val = DateUtils.parseDateToStr(dateFormat, (Date) val);
val = this.parseDateToStr(dateFormat, (Date) val);
}
else
{
@ -701,7 +703,7 @@ public class ExcelUtil<T> @@ -701,7 +703,7 @@ public class ExcelUtil<T>
String separator = attr.separator();
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
{
cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
cell.setCellValue(this.parseDateToStr(dateFormat, (Date) value));
}
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
{
@ -1154,4 +1156,29 @@ public class ExcelUtil<T> @@ -1154,4 +1156,29 @@ public class ExcelUtil<T>
}
return true;
}
/**
* 增加ExcelUtil对java8 日期的支持
* 格式化日期日期可能是{@link Date}{@link LocalDateTime} {@link LocalDate} 其他日期暂不支持
* @param dateFormat 日期格式
* @param val 被格式化的日期对象
* @see DateUtils#parseDateToStr(String, Date)
*/
private String parseDateToStr(final String dateFormat, Object val)
{
if (val == null) {
return "";
}
String str;
if (val instanceof Date) {
str = DateUtils.parseDateToStr(dateFormat, (Date) val);
} else if (val instanceof LocalDateTime) {
str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDateTime) val));
} else if (val instanceof LocalDate) {
str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDate) val));
} else {
str = val.toString();
}
return str;
}
}
Loading…
Cancel
Save