import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* 日期格式化工具
* @author Administrator
*
*/
public class DateUtil {
public static Date format(String date) {
if(date.matches("[a-zA-Z]{3} [a-zA-Z]{3} \\d{1,2} \\d{1,2}:\\d{1,2}:\\d{2} CST \\d{4}")) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss 'CST' yyyy", Locale.ENGLISH);
try {
return dateFormat.parse(date);
} catch (ParseException e) {
}
}else if(date.matches("[a-zA-Z]{3} \\d{2}, \\d{4} \\d{1,2}:\\d{1,2}:\\d{1,2} (AM)|(PM)")) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy h:m:s aa", Locale.ENGLISH);
try {
return dateFormat.parse(date);
} catch (ParseException e) {
}
}
//日期提取
String datePattern = "\\d{4}[-/年]{1}\\d{1,2}[-/月]{1}\\d{1,2}日?";
Pattern pattern = Pattern.compile(datePattern);
Matcher matcher = pattern.matcher(date);
String datepart = "";
if(matcher.find()) {
String extractDate = matcher.group();
String[] ymd = extractDate.split("[-/年月日]");
int month = Integer.valueOf(ymd[1]);
int day = Integer.valueOf(ymd[2]);
if((month>0 && month<13)&&(day>0 && day<32)) {
extractDate = ymd[0]+"-"+String.format("%02d", Integer.valueOf(ymd[1]))+"-"+String.format("%02d", Integer.valueOf(ymd[2]));
datepart = extractDate;
}
}
//时间提取
datePattern = "\\d{1,2}[::时]{1}\\d{1,2}[::分]{1}\\d{1,2}秒?";
pattern = Pattern.compile(datePattern);
matcher = pattern.matcher(date);
String timepart = "00:00:00";
if(matcher.find()) {
String extractDate = matcher.group();
String[] ymd = extractDate.split("[::时分秒]");
int hour = Integer.valueOf(ymd[0]);
int minute = Integer.valueOf(ymd[1]);
int second = Integer.valueOf(ymd[2]);
if((hour>-1&&hour<25)&&(minute>-1&&minute<60)&&(second>-1&&second<60)) {
extractDate = String.format("%02d", Integer.valueOf(ymd[0]))+":"+String.format("%02d", Integer.valueOf(ymd[1]))+":"+String.format("%02d", Integer.valueOf(ymd[2]));
timepart = extractDate;
}
}
boolean distinguishAMPM = false;
datePattern = "[amAM]{2}|[pmPM]{2}";
pattern = Pattern.compile(datePattern);
matcher = pattern.matcher(date);
if(matcher.find()) {
distinguishAMPM = true;
timepart += (" "+matcher.group());
}
if(StringUtils.isNotEmpty(datepart) && StringUtils.isNotEmpty(timepart)) {
String dateTime = datepart+" "+timepart;
SimpleDateFormat sdf = null;
String formarter = "";
if(distinguishAMPM) {
formarter = "yyyy-MM-dd hh:mm:ss a";
sdf = new SimpleDateFormat(formarter, Locale.ENGLISH);
}else {
formarter = "yyyy-MM-dd HH:mm:ss";
sdf = new SimpleDateFormat(formarter);
}
try {
return sdf.parse(dateTime);
} catch (ParseException e) {
}
}
return null;
}
public static void main(String[] args) {
System.out.println(format("2018/12/3 22时12分3秒"));
System.out.println(format("2018/2/30 22:2:30"));
System.out.println(format("2018/2/30 22:2:30"));
System.out.println(format("2018-2-30 22:2:30"));
System.out.println(format("2018年2月30日 22:2:30"));
System.out.println(format("2018/2/30干扰项22:2:30 am"));
}
}
网友评论