!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.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
/**
*
*
*
* @author ruoyi
*/
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 YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
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"};
/**
* Date
*
*
* @return Date()
*/
public static Date getNowDate()
@ -40,7 +41,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/**
* , yyyy-MM-dd
*
*
* @return String
*/
public static String getDate()
@ -121,7 +122,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
return null;
}
}
/**
*
*/
@ -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());
}
}

@ -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;
@ -56,7 +58,7 @@ import com.ruoyi.common.core.utils.reflect.ReflectUtils;
/**
* Excel
*
*
* @author ruoyi
*/
public class ExcelUtil<T>
@ -109,7 +111,7 @@ public class ExcelUtil<T>
*
*/
private int rownum;
/**
*
*/
@ -174,7 +176,7 @@ public class ExcelUtil<T>
/**
* excellist
*
*
* @param is
* @return
*/
@ -185,7 +187,7 @@ public class ExcelUtil<T>
/**
* excellist
*
*
* @param is
* @param titleNum
* @return
@ -197,7 +199,7 @@ public class ExcelUtil<T>
/**
* excellist
*
*
* @param sheetName
* @param titleNum
* @param is
@ -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
{
@ -351,7 +353,7 @@ public class ExcelUtil<T>
/**
* listexcel
*
*
* @param response
* @param list
* @param sheetName
@ -365,7 +367,7 @@ public class ExcelUtil<T>
/**
* listexcel
*
*
* @param response
* @param list
* @param sheetName
@ -383,13 +385,13 @@ public class ExcelUtil<T>
/**
* listexcel
*
*
* @param sheetName
* @return
*/
/**
* listexcel
*
*
* @param sheetName
* @return
*/
@ -400,7 +402,7 @@ public class ExcelUtil<T>
/**
* listexcel
*
*
* @param sheetName
* @param title
* @return
@ -415,7 +417,7 @@ public class ExcelUtil<T>
/**
* listexcel
*
*
* @return
*/
public void exportExcel(HttpServletResponse response)
@ -465,7 +467,7 @@ public class ExcelUtil<T>
/**
* excel
*
*
* @param index
* @param row
*/
@ -490,7 +492,7 @@ public class ExcelUtil<T>
/**
*
*
*
* @param wb
* @return
*/
@ -582,7 +584,7 @@ public class ExcelUtil<T>
/**
*
*
*
* @param value
* @param attr
* @param cell
@ -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))
{
@ -732,7 +734,7 @@ public class ExcelUtil<T>
/**
* POI XSSFSheet
*
*
* @param sheet
* @param promptTitle
* @param promptContent
@ -755,7 +757,7 @@ public class ExcelUtil<T>
/**
* ,.
*
*
* @param sheet sheet.
* @param textlist
* @param firstRow
@ -789,7 +791,7 @@ public class ExcelUtil<T>
/**
* 0=,1=,2=
*
*
* @param propertyValue
* @param converterExp
* @param separator
@ -826,7 +828,7 @@ public class ExcelUtil<T>
/**
* =0,=1,=2
*
*
* @param propertyValue
* @param converterExp
* @param separator
@ -863,7 +865,7 @@ public class ExcelUtil<T>
/**
*
*
*
* @param value
* @param excel
* @return
@ -931,7 +933,7 @@ public class ExcelUtil<T>
/**
* bean
*
*
* @param vo
* @param field
* @param excel
@ -962,7 +964,7 @@ public class ExcelUtil<T>
/**
* get
*
*
* @param o
* @param name
* @return value
@ -1057,7 +1059,7 @@ public class ExcelUtil<T>
/**
*
*
*
* @param sheetNo sheet
* @param index
*/
@ -1074,7 +1076,7 @@ public class ExcelUtil<T>
/**
*
*
*
* @param row
* @param column
* @return
@ -1134,7 +1136,7 @@ public class ExcelUtil<T>
/**
*
*
*
* @param row
* @return
*/
@ -1154,4 +1156,29 @@ public class ExcelUtil<T>
}
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