Skip to content

Commit

Permalink
优化Excel格式化不同类型的日期对象
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzongzhuan committed Feb 26, 2022
1 parent 034e7ec commit b99efad
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
28 changes: 26 additions & 2 deletions ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import java.lang.management.ManagementFactory;
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.ZonedDateTime;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;

Expand All @@ -22,7 +27,7 @@ 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",
Expand Down Expand Up @@ -121,7 +126,7 @@ public static Date parseDate(Object str)
return null;
}
}

/**
* 获取服务器启动时间
*/
Expand Down Expand Up @@ -160,4 +165,23 @@ public static String getDatePoor(Date endDate, Date nowDate)
// 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
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;
Expand Down Expand Up @@ -132,7 +134,7 @@ public class ExcelUtil<T>
* 当前行号
*/
private int rownum;

/**
* 标题
*/
Expand Down Expand Up @@ -314,7 +316,7 @@ public List<T> importExcel(String sheetName, InputStream is, int titleNum) throw
String dateFormat = field.getAnnotation(Excel.class).dateFormat();
if (StringUtils.isNotEmpty(dateFormat))
{
val = DateUtils.parseDateToStr(dateFormat, (Date) val);
val = parseDateToStr(dateFormat, (Date) val);
}
else
{
Expand Down Expand Up @@ -409,7 +411,7 @@ public AjaxResult exportExcel(List<T> list, String sheetName)
{
return exportExcel(list, sheetName, StringUtils.EMPTY);
}

/**
* 对list数据源将其里面的数据导入到excel表单
*
Expand Down Expand Up @@ -821,7 +823,7 @@ public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
String dictType = attr.dictType();
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
{
cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
cell.setCellValue(parseDateToStr(dateFormat, (Date) value));
}
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
{
Expand Down Expand Up @@ -1394,4 +1396,37 @@ public static Map<String, PictureData> getSheetPictures07(XSSFSheet sheet, XSSFW
}
return sheetIndexPicMap;
}

/**
* 格式化不同类型的日期对象
*
* @param dateFormat 日期格式
* @param val 被格式化的日期对象
* @return 格式化后的日期字符
*/
public String parseDateToStr(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;
}
}

0 comments on commit b99efad

Please sign in to comment.