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

Merge pull request !160 from dazer007/excelUtil_add_java8_date
pull/159/MERGE
若依 3 years ago committed by Gitee
commit e059ed9c7f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -3,12 +3,13 @@ package com.ruoyi.common.core.utils;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date; import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateFormatUtils;
/** /**
* *
* *
* @author ruoyi * @author ruoyi
*/ */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils public class DateUtils extends org.apache.commons.lang3.time.DateUtils
@ -22,15 +23,15 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = { private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/** /**
* Date * Date
* *
* @return Date() * @return Date()
*/ */
public static Date getNowDate() public static Date getNowDate()
@ -40,7 +41,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* , yyyy-MM-dd * , yyyy-MM-dd
* *
* @return String * @return String
*/ */
public static String getDate() public static String getDate()
@ -121,7 +122,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
return null; return null;
} }
} }
/** /**
* *
*/ */
@ -152,4 +153,23 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
// long sec = diff % nd % nh % nm / ns; // long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟"; 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());
}
} }

@ -6,6 +6,8 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -56,7 +58,7 @@ import com.ruoyi.common.core.utils.reflect.ReflectUtils;
/** /**
* Excel * Excel
* *
* @author ruoyi * @author ruoyi
*/ */
public class ExcelUtil<T> public class ExcelUtil<T>
@ -109,7 +111,7 @@ public class ExcelUtil<T>
* *
*/ */
private int rownum; private int rownum;
/** /**
* *
*/ */
@ -174,7 +176,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param is * @param is
* @return * @return
*/ */
@ -185,7 +187,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param is * @param is
* @param titleNum * @param titleNum
* @return * @return
@ -197,7 +199,7 @@ public class ExcelUtil<T>
/** /**
* excellist * excellist
* *
* @param sheetName * @param sheetName
* @param titleNum * @param titleNum
* @param is * @param is
@ -282,7 +284,7 @@ public class ExcelUtil<T>
String dateFormat = field.getAnnotation(Excel.class).dateFormat(); String dateFormat = field.getAnnotation(Excel.class).dateFormat();
if (StringUtils.isNotEmpty(dateFormat)) if (StringUtils.isNotEmpty(dateFormat))
{ {
val = DateUtils.parseDateToStr(dateFormat, (Date) val); val = this.parseDateToStr(dateFormat, (Date) val);
} }
else else
{ {
@ -351,7 +353,7 @@ public class ExcelUtil<T>
/** /**
* listexcel * listexcel
* *
* @param response * @param response
* @param list * @param list
* @param sheetName * @param sheetName
@ -365,7 +367,7 @@ public class ExcelUtil<T>
/** /**
* listexcel * listexcel
* *
* @param response * @param response
* @param list * @param list
* @param sheetName * @param sheetName
@ -383,13 +385,13 @@ public class ExcelUtil<T>
/** /**
* listexcel * listexcel
* *
* @param sheetName * @param sheetName
* @return * @return
*/ */
/** /**
* listexcel * listexcel
* *
* @param sheetName * @param sheetName
* @return * @return
*/ */
@ -400,7 +402,7 @@ public class ExcelUtil<T>
/** /**
* listexcel * listexcel
* *
* @param sheetName * @param sheetName
* @param title * @param title
* @return * @return
@ -415,7 +417,7 @@ public class ExcelUtil<T>
/** /**
* listexcel * listexcel
* *
* @return * @return
*/ */
public void exportExcel(HttpServletResponse response) public void exportExcel(HttpServletResponse response)
@ -465,7 +467,7 @@ public class ExcelUtil<T>
/** /**
* excel * excel
* *
* @param index * @param index
* @param row * @param row
*/ */
@ -490,7 +492,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param wb * @param wb
* @return * @return
*/ */
@ -582,7 +584,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param value * @param value
* @param attr * @param attr
* @param cell * @param cell
@ -701,7 +703,7 @@ public class ExcelUtil<T>
String separator = attr.separator(); String separator = attr.separator();
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) 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)) else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
{ {
@ -732,7 +734,7 @@ public class ExcelUtil<T>
/** /**
* POI XSSFSheet * POI XSSFSheet
* *
* @param sheet * @param sheet
* @param promptTitle * @param promptTitle
* @param promptContent * @param promptContent
@ -755,7 +757,7 @@ public class ExcelUtil<T>
/** /**
* ,. * ,.
* *
* @param sheet sheet. * @param sheet sheet.
* @param textlist * @param textlist
* @param firstRow * @param firstRow
@ -789,7 +791,7 @@ public class ExcelUtil<T>
/** /**
* 0=,1=,2= * 0=,1=,2=
* *
* @param propertyValue * @param propertyValue
* @param converterExp * @param converterExp
* @param separator * @param separator
@ -826,7 +828,7 @@ public class ExcelUtil<T>
/** /**
* =0,=1,=2 * =0,=1,=2
* *
* @param propertyValue * @param propertyValue
* @param converterExp * @param converterExp
* @param separator * @param separator
@ -863,7 +865,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param value * @param value
* @param excel * @param excel
* @return * @return
@ -931,7 +933,7 @@ public class ExcelUtil<T>
/** /**
* bean * bean
* *
* @param vo * @param vo
* @param field * @param field
* @param excel * @param excel
@ -962,7 +964,7 @@ public class ExcelUtil<T>
/** /**
* get * get
* *
* @param o * @param o
* @param name * @param name
* @return value * @return value
@ -1057,7 +1059,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param sheetNo sheet * @param sheetNo sheet
* @param index * @param index
*/ */
@ -1074,7 +1076,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param row * @param row
* @param column * @param column
* @return * @return
@ -1134,7 +1136,7 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @param row * @param row
* @return * @return
*/ */
@ -1154,4 +1156,29 @@ public class ExcelUtil<T>
} }
return true; return true;
} }
}
/**
* ExcelUtiljava8
* {@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