【fix sonar】DateUtils文件

This commit is contained in:
梁琦涛
2023-03-03 16:35:37 +08:00
parent b342d29bf9
commit 4deffce53b
@@ -13,6 +13,7 @@ import java.time.ZonedDateTime;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Objects;
import org.springframework.util.StringUtils; 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 DAY_IN_MILLIS = 24 * 3600 * 1000L;
private static final long HOUR_IN_MILLIS = 3600 * 1000; private static final long HOUR_IN_MILLIS = 3600 * 1000L;
private static final long MINUTE_IN_MILLIS = 60 * 1000; private static final long MINUTE_IN_MILLIS = 60 * 1000L;
private static final long SECOND_IN_MILLIS = 1000; private static final long SECOND_IN_MILLIS = 1000L;
// 指定模式的时间格式 // 指定模式的时间格式
private static SimpleDateFormat getSDFormat(String pattern) { private static SimpleDateFormat getSDFormat(String pattern) {
@@ -96,7 +97,6 @@ public class DateUtils extends PropertyEditorSupport {
*/ */
public static Calendar getCalendar(long millis) { public static Calendar getCalendar(long millis) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
// --------------------cal.setTimeInMillis(millis);
cal.setTime(new Date(millis)); cal.setTime(new Date(millis));
return cal; return cal;
} }
@@ -128,14 +128,9 @@ public class DateUtils extends PropertyEditorSupport {
/** /**
* 时间戳转换为字符串 * 时间戳转换为字符串
* *
* @param time
* @return * @return
*/ */
public static String timestamptoStr(Timestamp time) { public static String timestamptoStr() {
Date date = null;
if (null != time) {
date = new Date(time.getTime());
}
return date2Str(date_sdf.get()); return date2Str(date_sdf.get());
} }
@@ -147,6 +142,9 @@ public class DateUtils extends PropertyEditorSupport {
*/ */
public static Timestamp str2Timestamp(String str) { public static Timestamp str2Timestamp(String str) {
Date date = str2Date(str, date_sdf.get()); Date date = str2Date(str, date_sdf.get());
if(Objects.isNull(date)){
return null;
}
return new Timestamp(date.getTime()); return new Timestamp(date.getTime());
} }
@@ -179,9 +177,6 @@ public class DateUtils extends PropertyEditorSupport {
*/ */
public static String date2Str(SimpleDateFormat date_sdf) { public static String date2Str(SimpleDateFormat date_sdf) {
Date date = getDate(); Date date = getDate();
if (null == date) {
return null;
}
return date_sdf.format(date); return date_sdf.format(date);
} }
@@ -198,7 +193,6 @@ public class DateUtils extends PropertyEditorSupport {
try { try {
_date = sformat.parse(date); _date = sformat.parse(date);
} catch (ParseException e) { } catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
return sformat.format(_date); return sformat.format(_date);
@@ -226,9 +220,6 @@ public class DateUtils extends PropertyEditorSupport {
*/ */
public static String getDate(String format) { public static String getDate(String format) {
Date date = new Date(); Date date = new Date();
if (null == date) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format); SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date); return sdf.format(date);
} }
@@ -288,11 +279,10 @@ public class DateUtils extends PropertyEditorSupport {
* @return 指定日历的时间戳 * @return 指定日历的时间戳
*/ */
public static Timestamp getCalendarTimestamp(Calendar cal) { public static Timestamp getCalendarTimestamp(Calendar cal) {
// ---------------------return new Timestamp(cal.getTimeInMillis());
return new Timestamp(cal.getTime().getTime()); return new Timestamp(cal.getTime().getTime());
} }
public static Timestamp gettimestamp() { public static Timestamp geTimestamps() {
Date dt = new Date(); Date dt = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowTime = df.format(dt); String nowTime = df.format(dt);
@@ -321,7 +311,6 @@ public class DateUtils extends PropertyEditorSupport {
* @return 指定日历的毫秒数 * @return 指定日历的毫秒数
*/ */
public static long getMillis(Calendar cal) { public static long getMillis(Calendar cal) {
// --------------------return cal.getTimeInMillis();
return cal.getTime().getTime(); return cal.getTime().getTime();
} }
@@ -635,7 +624,7 @@ public class DateUtils extends PropertyEditorSupport {
try { try {
if (text.indexOf(":") == -1 && text.length() == 10) { if (text.indexOf(":") == -1 && text.length() == 10) {
setValue(DateUtils.date_sdf.get().parse(text)); 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)); setValue(DateUtils.datetimeFormat.get().parse(text));
} else { } else {
throw new IllegalArgumentException("Could not parse date, date format is error "); throw new IllegalArgumentException("Could not parse date, date format is error ");