From fefcae26d7f5bf6d0ff8e22a08449e2652f2fc3a Mon Sep 17 00:00:00 2001 From: RuoYi Date: Tue, 13 Nov 2018 13:07:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E5=8F=B0=E6=97=A5=E6=9C=9F=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E8=BD=AC=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/web/core/base/BaseController.java | 16 ++++++++---- .../com/ruoyi/common/utils/DateUtils.java | 26 ++++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/base/BaseController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/base/BaseController.java index 8c61e436..a1bdfc31 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/core/base/BaseController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/base/BaseController.java @@ -1,14 +1,14 @@ package com.ruoyi.web.core.base; -import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; -import org.springframework.beans.propertyeditors.CustomDateEditor; +import java.beans.PropertyEditorSupport; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.ruoyi.common.base.AjaxResult; +import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.framework.web.page.PageDomain; @@ -29,9 +29,15 @@ public class BaseController @InitBinder public void initBinder(WebDataBinder binder) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - dateFormat.setLenient(false); - binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); + // Date 类型转换 + binder.registerCustomEditor(Date.class, new PropertyEditorSupport() + { + @Override + public void setAsText(String text) + { + setValue(DateUtils.parseDate(text)); + } + }); } /** diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java index 3e4d5f5b..996bdd34 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java @@ -10,7 +10,7 @@ import org.apache.commons.lang3.time.DateFormatUtils; * * @author ruoyi */ -public class DateUtils +public class DateUtils extends org.apache.commons.lang3.time.DateUtils { public static String YYYY = "yyyy"; @@ -21,6 +21,11 @@ public class 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"}; /** * 获取当前Date型日期 @@ -96,4 +101,23 @@ public class DateUtils Date now = new Date(); return DateFormatUtils.format(now, "yyyyMMdd"); } + + /** + * 日期型字符串转化为日期 格式 + */ + public static Date parseDate(Object str) + { + if (str == null) + { + return null; + } + try + { + return parseDate(str.toString(), parsePatterns); + } + catch (ParseException e) + { + return null; + } + } }