前言
最近在优化代码方面,因为之前在字符串中截取日期,日期的格式样式不统一。
日期格式
2019/6/14 14:00:00 2019/03/4 1:00:00 2019年8月4日12:00
要求改成统一格式 : 2019-06-08 12:00:00
把斜线改成横线
String strDate
= "2019/6/14 14:00";
Date str
= DateUtil
.stringToDate(strDate
.replace("/", "-")
, DateExtendUtil
.FULL
);
String strDate
= "2019年6月14日 14:00 ";
String str
= strDate
.replaceAll("[年,月]", "-");
Date str
= DateUtil
.stringToDate(str
.replace("[日]", "-")
+ ":00" , DateExtendUtil
.FULL
);
时间处理类
public class DateExtendUtil extends DateUtil {
public static final String FULL
= "yyyy-MM-dd HH:mm:ss";
private static final long ONE_DAY
= 24 * 60 * 60 * 1000L
;
public static Date
stringToDate(String strDate
, String formartStr
) {
Date date
= null
;
if (formartStr
!= null
&& !"".equals(formartStr
)) {
SimpleDateFormat sdf
= new SimpleDateFormat(formartStr
);
try {
date
= sdf
.parse(strDate
);
} catch (Exception ignored
) {
}
}
return date
;
}
}