【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.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();
}
}
}