easyexcel功能扩展:读取excel日期格式(包括日期格式和文本格式)

12 篇文章 0 订阅

 实体类属性

    @ExcelProperty(value = "时间", index = 11)
    private String time;

调用DateUtil

        DateUtil.setParams("yyyy/MM/dd","yyyy-MM-dd");
        DateUtil.formatExcelDate(ExcelImport.getTime())

DateUtil

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    static private MyDateFormat myDateFormat;

    static private String format = "yyyy-MM-dd";

    static private String storageFormat = "yyyy-MM-dd";

    static private boolean easy = false;

    public static MyDateFormat getMyDateFormat(String format) {
        DateUtil.myDateFormat = new MyDateFormat(format);
        return DateUtil.myDateFormat;
    }

    public static void setParams(String format, String storageFormat, boolean easy) {
        DateUtil.format = format;
        DateUtil.storageFormat = storageFormat;
        DateUtil.easy = easy;
    }

    public static void setParams(String format, String storageFormat) {
        DateUtil.format = format;
        DateUtil.storageFormat = storageFormat;
    }

    /**
     * 格式化Excel时间(yyyy-MM-dd)
     *
     * @param str
     * @return
     */
    public static String formatExcelDate(String str) {
        //数据的格式
        int yearIndex = StringUtils.indexOf(format, "yyyy");
        int monthIndex = StringUtils.indexOf(format, "MM");
        int dayIndex = StringUtils.indexOf(format, "dd");
        String year = format.substring(yearIndex + 4, monthIndex);
        String month = format.substring(monthIndex + 2, dayIndex);
        //存储的格式
        int storageYearIndex = StringUtils.indexOf(storageFormat, "yyyy");
        int storageMonthIndex = StringUtils.indexOf(storageFormat, "MM");
        int storageDayIndex = StringUtils.indexOf(storageFormat, "dd");
        String storageYear = storageFormat.substring(storageYearIndex + 4, storageMonthIndex);
        String storageMonth = storageFormat.substring(storageMonthIndex + 2, storageDayIndex);
        if (StringUtils.isBlank(str)) {
            return "";
        } else if (StringUtils.isNumeric(str)) {
            /*数字*/
            Calendar calendar = new GregorianCalendar(1900, 0, -1);
            Date gregorianDate = calendar.getTime();
            return DateUtil.getMyDateFormat(storageFormat).format(DateUtils.addDays(gregorianDate, Integer.parseInt(str)));
        } else if (str.matches("^(\\d{4}" + year + "\\d{1,2})$") && easy) {
            /*yyyy/MM*/
            Date date = DateUtil.getMyDateFormat("yyyy" + year + "MM").parse(str);
            return DateUtil.getMyDateFormat("yyyy" + storageYear + "MM").format(date);
        } else if (str.matches("^(\\d{4}" + year + "\\d{1,2}" + month + "\\d{1,2})$")) {
            /*yyyy/MM/dd*/
            Date date = DateUtil.getMyDateFormat("yyyy" + year + "MM" + month + "dd").parse(str);
            return DateUtil.getMyDateFormat("yyyy" + storageYear + "MM" + storageMonth + "dd").format(date);
        } else {
            return "";
        }
    }

    public static String formatExcelDate(String str, boolean easyTemp) {
        //数据的格式
        int yearIndex = StringUtils.indexOf(format, "yyyy");
        int monthIndex = StringUtils.indexOf(format, "MM");
        int dayIndex = StringUtils.indexOf(format, "dd");
        String year = format.substring(yearIndex + 4, monthIndex);
        String month = format.substring(monthIndex + 2, dayIndex);
        //存储的格式
        int storageYearIndex = StringUtils.indexOf(storageFormat, "yyyy");
        int storageMonthIndex = StringUtils.indexOf(storageFormat, "MM");
        int storageDayIndex = StringUtils.indexOf(storageFormat, "dd");
        String storageYear = storageFormat.substring(storageYearIndex + 4, storageMonthIndex);
        String storageMonth = storageFormat.substring(storageMonthIndex + 2, storageDayIndex);
        if (StringUtils.isBlank(str)) {
            return "";
        } else if (StringUtils.isNumeric(str)) {
            /*数字*/
            Calendar calendar = new GregorianCalendar(1900, 0, -1);
            Date gregorianDate = calendar.getTime();
            return DateUtil.getMyDateFormat(storageFormat).format(DateUtils.addDays(gregorianDate, Integer.parseInt(str)));
        } else if (str.matches("^(\\d{4}" + year + "\\d{1,2})$") && easyTemp) {
            /*yyyy/MM*/
            Date date = DateUtil.getMyDateFormat("yyyy" + year + "MM").parse(str);
            return DateUtil.getMyDateFormat("yyyy" + storageYear + "MM").format(date);
        } else if (str.matches("^(\\d{4}" + year + "\\d{1,2}" + month + "\\d{1,2})$")) {
            /*yyyy/MM/dd*/
            Date date = DateUtil.getMyDateFormat("yyyy" + year + "MM" + month + "dd").parse(str);
            return DateUtil.getMyDateFormat("yyyy" + storageYear + "MM" + storageMonth + "dd").format(date);
        } else {
            return "";
        }
    }

    /**
     * 自动填充0
     *
     * @param format
     * @param str
     * @return
     */
    public static String autoAdd0(String format, String str) {
        Date date = DateUtil.getMyDateFormat(format).parse(str);
        return DateUtil.getMyDateFormat(format).format(date);
    }

    /*日期内部类*/
    public static class MyDateFormat {
        private SimpleDateFormat simpleDateFormat;

        public MyDateFormat(String format) {
            this.simpleDateFormat = new SimpleDateFormat(format);
        }

        public String format(Date date) {
            if (date == null) {
                return "";
            } else {
                return simpleDateFormat.format(date);
            }
        }

        public Date parse(String str) {
            if (str == null) {
                return null;
            } else {
                try {
                    return simpleDateFormat.parse(str);
                } catch (ParseException e) {
                    return null;
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页
评论

打赏作者

上单法师刘海柱

反正也没人打赏我

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值