|
|
@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
package cn.estsh.i3plus.pojo.mes.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DateTimeUtil {
|
|
|
|
|
|
|
|
public static final String DATETIME_FORMATOR = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
|
|
|
public static final String DATE_FORMATOR = "yyyy-MM-dd";
|
|
|
|
|
|
|
|
public static final String TIME_FORMATOR = "HH:mm:ss";
|
|
|
|
|
|
|
|
public static final String SHORT_EN_FORMAT = "yyyyMMdd";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String formatDate(Date date) {
|
|
|
|
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat(DATETIME_FORMATOR);
|
|
|
|
|
|
|
|
return format.format(date);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Date parse(String dateStr) {
|
|
|
|
|
|
|
|
if (dateStr.length() == 16) {
|
|
|
|
|
|
|
|
return parse(dateStr, DATETIME_FORMATOR);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateStr.length() == 10) {
|
|
|
|
|
|
|
|
return parse(dateStr, DATE_FORMATOR);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return parse(dateStr, DATETIME_FORMATOR);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Date parse(String dateStr, String formatStr) {
|
|
|
|
|
|
|
|
return parse(dateStr, formatStr, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Date parse(String dateStr, String formatStr, Date defaultDate) {
|
|
|
|
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat(formatStr);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
return format.parse(dateStr);
|
|
|
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
|
|
|
return defaultDate;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isValidDate(String dateStr) {
|
|
|
|
|
|
|
|
//判断结果 默认为true
|
|
|
|
|
|
|
|
boolean judgeresult = true;
|
|
|
|
|
|
|
|
//1、首先使用SimpleDateFormat初步进行判断,过滤掉注入 yyyy-01-32 或yyyy-00-0x等格式
|
|
|
|
|
|
|
|
//此处可根据实际需求进行调整,如需判断yyyy/MM/dd格式将参数改掉即可
|
|
|
|
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat(DATETIME_FORMATOR);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
//增加强判断条件,否则 诸如2022-02-29也可判断出去
|
|
|
|
|
|
|
|
format.setLenient(false);
|
|
|
|
|
|
|
|
Date date = parse(formatDate(DATETIME_FORMATOR, parse(dateStr)));
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
System.out.println(date);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
judgeresult = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//由于上述方法只能验证正常的日期格式,像诸如 0001-01-01、11-01-01,10001-01-01等无法校验,此处再添加校验年费是否合法
|
|
|
|
|
|
|
|
String yearStr = dateStr.split("-")[0];
|
|
|
|
|
|
|
|
if (yearStr.startsWith("0") || yearStr.length() != 4) {
|
|
|
|
|
|
|
|
judgeresult = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return judgeresult;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String formatDate(String formatter, Date date) {
|
|
|
|
|
|
|
|
if (date == null) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat(formatter);
|
|
|
|
|
|
|
|
return format.format(date);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|