String dateString = "2018-08-05T16:00:00.000Z";
public static String getDateFormat(String create_time) throws ParseException{
String format = "";
if (create_time != null && create_time != "NULL" && create_time != "") {
if (isDate(create_time)) {
format = create_time;
} else {
//转换日期格式(将Mon Jun 18 2018 00:00:00 GMT+0800 (中国标准时间) 转换成yyyy-MM-dd)
create_time = create_time.replace("Z", " UTC");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Date d = sdf1.parse(create_time);//Mon Mar 06 00:00:00 CST 2017
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
format = sdf.format(d);//2017-03-06
}
}
return format;
}
private static boolean isDate(String date)
{
/**
* 判断日期格式和范围
*/
String rexp = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(date);
boolean dateType = mat.matches();
return dateType;
}
2018-08-05T16:00:00.000Z 转成yyyy-MM-dd格式
猜你喜欢
转载自blog.csdn.net/qq_32027105/article/details/81459508
今日推荐
周排行