作者 RuoYi

优化Excel格式化不同类型的日期对象

@@ -3,6 +3,11 @@ package com.ruoyi.common.utils; @@ -3,6 +3,11 @@ package com.ruoyi.common.utils;
3 import java.lang.management.ManagementFactory; 3 import java.lang.management.ManagementFactory;
4 import java.text.ParseException; 4 import java.text.ParseException;
5 import java.text.SimpleDateFormat; 5 import java.text.SimpleDateFormat;
  6 +import java.time.LocalDate;
  7 +import java.time.LocalDateTime;
  8 +import java.time.LocalTime;
  9 +import java.time.ZoneId;
  10 +import java.time.ZonedDateTime;
6 import java.util.Date; 11 import java.util.Date;
7 import org.apache.commons.lang3.time.DateFormatUtils; 12 import org.apache.commons.lang3.time.DateFormatUtils;
8 13
@@ -132,6 +137,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils @@ -132,6 +137,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
132 } 137 }
133 138
134 /** 139 /**
  140 + * 计算相差天数
  141 + */
  142 + public static int differentDaysByMillisecond(Date date1, Date date2)
  143 + {
  144 + return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
  145 + }
  146 +
  147 + /**
135 * 计算两个时间差 148 * 计算两个时间差
136 */ 149 */
137 public static String getDatePoor(Date endDate, Date nowDate) 150 public static String getDatePoor(Date endDate, Date nowDate)
@@ -152,4 +165,23 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils @@ -152,4 +165,23 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
152 // long sec = diff % nd % nh % nm / ns; 165 // long sec = diff % nd % nh % nm / ns;
153 return day + "天" + hour + "小时" + min + "分钟"; 166 return day + "天" + hour + "小时" + min + "分钟";
154 } 167 }
  168 +
  169 + /**
  170 + * 增加 LocalDateTime ==> Date
  171 + */
  172 + public static Date toDate(LocalDateTime temporalAccessor)
  173 + {
  174 + ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
  175 + return Date.from(zdt.toInstant());
  176 + }
  177 +
  178 + /**
  179 + * 增加 LocalDate ==> Date
  180 + */
  181 + public static Date toDate(LocalDate temporalAccessor)
  182 + {
  183 + LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
  184 + ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
  185 + return Date.from(zdt.toInstant());
  186 + }
155 } 187 }
@@ -9,6 +9,8 @@ import java.lang.reflect.Field; @@ -9,6 +9,8 @@ import java.lang.reflect.Field;
9 import java.lang.reflect.Method; 9 import java.lang.reflect.Method;
10 import java.math.BigDecimal; 10 import java.math.BigDecimal;
11 import java.text.DecimalFormat; 11 import java.text.DecimalFormat;
  12 +import java.time.LocalDate;
  13 +import java.time.LocalDateTime;
12 import java.util.ArrayList; 14 import java.util.ArrayList;
13 import java.util.Arrays; 15 import java.util.Arrays;
14 import java.util.Comparator; 16 import java.util.Comparator;
@@ -314,7 +316,7 @@ public class ExcelUtil<T> @@ -314,7 +316,7 @@ public class ExcelUtil<T>
314 String dateFormat = field.getAnnotation(Excel.class).dateFormat(); 316 String dateFormat = field.getAnnotation(Excel.class).dateFormat();
315 if (StringUtils.isNotEmpty(dateFormat)) 317 if (StringUtils.isNotEmpty(dateFormat))
316 { 318 {
317 - val = DateUtils.parseDateToStr(dateFormat, (Date) val); 319 + val = parseDateToStr(dateFormat, (Date) val);
318 } 320 }
319 else 321 else
320 { 322 {
@@ -431,7 +433,6 @@ public class ExcelUtil<T> @@ -431,7 +433,6 @@ public class ExcelUtil<T>
431 * @param list 导出数据集合 433 * @param list 导出数据集合
432 * @param sheetName 工作表的名称 434 * @param sheetName 工作表的名称
433 * @return 结果 435 * @return 结果
434 - * @throws IOException  
435 */ 436 */
436 public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) 437 public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)
437 { 438 {
@@ -446,7 +447,6 @@ public class ExcelUtil<T> @@ -446,7 +447,6 @@ public class ExcelUtil<T>
446 * @param sheetName 工作表的名称 447 * @param sheetName 工作表的名称
447 * @param title 标题 448 * @param title 标题
448 * @return 结果 449 * @return 结果
449 - * @throws IOException  
450 */ 450 */
451 public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title) 451 public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title)
452 { 452 {
@@ -823,7 +823,7 @@ public class ExcelUtil<T> @@ -823,7 +823,7 @@ public class ExcelUtil<T>
823 String dictType = attr.dictType(); 823 String dictType = attr.dictType();
824 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) 824 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
825 { 825 {
826 - cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value)); 826 + cell.setCellValue(parseDateToStr(dateFormat, (Date) value));
827 } 827 }
828 else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) 828 else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
829 { 829 {
@@ -1396,4 +1396,37 @@ public class ExcelUtil<T> @@ -1396,4 +1396,37 @@ public class ExcelUtil<T>
1396 } 1396 }
1397 return sheetIndexPicMap; 1397 return sheetIndexPicMap;
1398 } 1398 }
  1399 +
  1400 + /**
  1401 + * 格式化不同类型的日期对象
  1402 + *
  1403 + * @param dateFormat 日期格式
  1404 + * @param val 被格式化的日期对象
  1405 + * @return 格式化后的日期字符
  1406 + */
  1407 + public String parseDateToStr(String dateFormat, Object val)
  1408 + {
  1409 + if (val == null)
  1410 + {
  1411 + return "";
  1412 + }
  1413 + String str;
  1414 + if (val instanceof Date)
  1415 + {
  1416 + str = DateUtils.parseDateToStr(dateFormat, (Date) val);
  1417 + }
  1418 + else if (val instanceof LocalDateTime)
  1419 + {
  1420 + str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDateTime) val));
  1421 + }
  1422 + else if (val instanceof LocalDate)
  1423 + {
  1424 + str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDate) val));
  1425 + }
  1426 + else
  1427 + {
  1428 + str = val.toString();
  1429 + }
  1430 + return str;
  1431 + }
1399 } 1432 }