From 4deffce53b5cc76217864868e1d1095e76183f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6=E6=B6=9B?= Date: Fri, 3 Mar 2023 16:35:37 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=20sonar=E3=80=91DateUtils=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jero/common/util/DateUtils.java | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DateUtils.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DateUtils.java index 4fc23f7c..8076967f 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DateUtils.java +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/DateUtils.java @@ -13,6 +13,7 @@ import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.Objects; import org.springframework.util.StringUtils; @@ -69,10 +70,10 @@ public class DateUtils extends PropertyEditorSupport { }; // 以毫秒表示的时间 - private static final long DAY_IN_MILLIS = 24 * 3600 * 1000; - private static final long HOUR_IN_MILLIS = 3600 * 1000; - private static final long MINUTE_IN_MILLIS = 60 * 1000; - private static final long SECOND_IN_MILLIS = 1000; + private static final long DAY_IN_MILLIS = 24 * 3600 * 1000L; + private static final long HOUR_IN_MILLIS = 3600 * 1000L; + private static final long MINUTE_IN_MILLIS = 60 * 1000L; + private static final long SECOND_IN_MILLIS = 1000L; // 指定模式的时间格式 private static SimpleDateFormat getSDFormat(String pattern) { @@ -96,7 +97,6 @@ public class DateUtils extends PropertyEditorSupport { */ public static Calendar getCalendar(long millis) { Calendar cal = Calendar.getInstance(); - // --------------------cal.setTimeInMillis(millis); cal.setTime(new Date(millis)); return cal; } @@ -128,14 +128,9 @@ public class DateUtils extends PropertyEditorSupport { /** * 时间戳转换为字符串 * - * @param time * @return */ - public static String timestamptoStr(Timestamp time) { - Date date = null; - if (null != time) { - date = new Date(time.getTime()); - } + public static String timestamptoStr() { return date2Str(date_sdf.get()); } @@ -147,6 +142,9 @@ public class DateUtils extends PropertyEditorSupport { */ public static Timestamp str2Timestamp(String str) { Date date = str2Date(str, date_sdf.get()); + if(Objects.isNull(date)){ + return null; + } return new Timestamp(date.getTime()); } @@ -179,9 +177,6 @@ public class DateUtils extends PropertyEditorSupport { */ public static String date2Str(SimpleDateFormat date_sdf) { Date date = getDate(); - if (null == date) { - return null; - } return date_sdf.format(date); } @@ -198,7 +193,6 @@ public class DateUtils extends PropertyEditorSupport { try { _date = sformat.parse(date); } catch (ParseException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return sformat.format(_date); @@ -226,9 +220,6 @@ public class DateUtils extends PropertyEditorSupport { */ public static String getDate(String format) { Date date = new Date(); - if (null == date) { - return null; - } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } @@ -288,11 +279,10 @@ public class DateUtils extends PropertyEditorSupport { * @return 指定日历的时间戳 */ public static Timestamp getCalendarTimestamp(Calendar cal) { - // ---------------------return new Timestamp(cal.getTimeInMillis()); return new Timestamp(cal.getTime().getTime()); } - public static Timestamp gettimestamp() { + public static Timestamp geTimestamps() { Date dt = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime = df.format(dt); @@ -321,7 +311,6 @@ public class DateUtils extends PropertyEditorSupport { * @return 指定日历的毫秒数 */ public static long getMillis(Calendar cal) { - // --------------------return cal.getTimeInMillis(); return cal.getTime().getTime(); } @@ -624,7 +613,7 @@ public class DateUtils extends PropertyEditorSupport { public static Long getCurrentTimestamp() { return Long.valueOf(DateUtils.yyyymmddhhmmss.get().format(new Date())); } - + /** * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd * HH:mm:ss“ * @param text String类型的时间值 @@ -635,7 +624,7 @@ public class DateUtils extends PropertyEditorSupport { try { if (text.indexOf(":") == -1 && text.length() == 10) { setValue(DateUtils.date_sdf.get().parse(text)); - } else if (text.indexOf(":") > 0 && text.length() == 19) { + } else if (text.indexOf(":") > -1 && text.length() == 19) { setValue(DateUtils.datetimeFormat.get().parse(text)); } else { throw new IllegalArgumentException("Could not parse date, date format is error "); @@ -704,4 +693,4 @@ public class DateUtils extends PropertyEditorSupport { return zonedDateTime.toLocalDateTime(); } -} \ No newline at end of file +}