1. 程式人生 > >Java將字串轉換為時間,支援多種格式

Java將字串轉換為時間,支援多種格式

適用格式

//2017/11/25    11:11:11
//2017-11-25 11:11:11
//2017.11.25 11:11:11
//2017/11/25
//2017-11-25
//2017.11.25


public String judgeFormat(String time){
		String regex1 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}$";
		String regex2 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}$";
		Pattern p1 = Pattern.compile(regex1);
		Matcher m1 = p1.matcher(time);
		if(m1.matches()){
			return "yyyy-MM-dd||yyyy.MM.dd||yyyy/MM/dd";
		}
		Pattern p2 = Pattern.compile(regex2);
		Matcher m2 = p2.matcher(time);
		if(m2.matches()){
			return "yyyy.MM.dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy-MM-dd HH:mm:ss";
		}
		return null;
	}
	
	public Date convertStringtoDate(String time) throws ParseException{
		Date date = null;
		if(judgeFormat(time).startsWith("yyyy-MM-dd")){
			time = convertString(time);
			date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
			return date;
		}else if(judgeFormat(time).endsWith("yyyy-MM-dd HH:mm:ss")){
			time = convertString(time);
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
			return date;
		}
		return null;
	}
	
	public String convertString(String time){
		if(time.contains("/")){
			time = time.replace("/", "-");
		}else if(time.contains(".")){
			time = time.replace(".", "-");
		}else {
			
		}
		return time;
	}
    正則表示式部分還需要修改.....
----------------------------------------------------------------------======================================------------------------------------------------------------------
    字串前後空格匹配,格式限定
	public String judgeFormat(String time){
//		String regex1 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}$";
//		String regex2 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}$";
		String regex1 = "^\\s+\\d{4}(-|/|\\.){1}\\d{2}(-|/|\\.){1}\\d{2}\\s+";
		String regex2 = "^\\s+\\d{4}(-|/|\\.){1}\\d{2}(-|/|\\.){1}\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}\\s+";
		Pattern p1 = Pattern.compile(regex1);
		Matcher m1 = p1.matcher(time);
		if(m1.matches()){
			return "yyyy-MM-dd||yyyy.MM.dd||yyyy/MM/dd";
		}
		Pattern p2 = Pattern.compile(regex2);
		Matcher m2 = p2.matcher(time);
		if(m2.matches()){
			return "yyyy.MM.dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy-MM-dd HH:mm:ss";
		}
		return "格式不符合要求";
	}
	
	public Date convertStringtoDate(String time) throws ParseException{
		Date date = null;
		if(judgeFormat(time).startsWith("yyyy-MM-dd")){
			time = convertString(time);
			date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
			return date;
		}else if(judgeFormat(time).endsWith("yyyy-MM-dd HH:mm:ss")){
			time = convertString(time);
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
			return date;
		}
		return null;
	}
	
	public String convertString(String time){
		if(time.contains("/")){
			time = time.replace("/", "-");
		}else if(time.contains(".")){
			time = time.replace(".", "-");
		}else {
			
		}
		return time;
	}

 前後空格0個或多個,增加中文字元匹配
public static final String FORMATYYYYMMMDD = "yyyy-MM-dd||yyyy.MM.dd||yyyy/MM/dd";
	public static final String FORMATYYYYMMDD_Z = "yyyy年MM月dd日";
	public static final String FORMATYYYYMMDDHHMMSS = "yyyy.MM.dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy-MM-dd HH:mm:ss";
	public static final String FORMATYYYYMMDDHHMMSS_Z = "yyyy年MM月dd日 HH時mm分ss秒";
	public static final String FORMATUNCR = "格式不符合要求";
	public String judgeFormat(String time){
//		String regex1 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}$";
//		String regex2 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}$";
		String regex1 = "^\\s*\\d{4}(-|/|\\.){1}\\d{2}(-|/|\\.){1}\\d{2}\\s*";
		String regex2 = "^\\s*\\d{4}(-|/|\\.){1}\\d{2}(-|/|\\.){1}\\d{2}\\s+(\\d{2}:){2}\\d{2}\\s*";
		String regex3 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}\\d{2}([\u4e00-\u9fa5]){1}\\d{2}([\u4e00-\u9fa5]){1}\\s*";
		String regex4 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}(\\d{2}([\u4e00-\u9fa5]){1}){2}\\s*(\\d{2}([\u4e00-\u9fa5]){1}){3}\\s*";
		Pattern p1 = Pattern.compile(regex1);
		Matcher m1 = p1.matcher(time);
		if(m1.matches()){
			return FORMATYYYYMMMDD;
		}
		Pattern p2 = Pattern.compile(regex2);
		Matcher m2 = p2.matcher(time);
		if(m2.matches()){
			return FORMATYYYYMMDDHHMMSS;
		}
		Pattern p3 = Pattern.compile(regex3);
		Matcher m3 = p3.matcher(time);
		if(m3.matches()){
			return FORMATYYYYMMDD_Z;
		}
		Pattern p4 = Pattern.compile(regex4);
		Matcher m4 = p4.matcher(time);
		if(m4.matches()){
			return FORMATYYYYMMDDHHMMSS_Z;
		}
		return FORMATUNCR;
	}
	
	public Date convertStringtoDate(String time) throws Exception{
		Date date = null;
		String format = judgeFormat(time);
		if(FORMATYYYYMMMDD.equals(format)||FORMATYYYYMMDD_Z.equals(format)){
			time = convertString(time);
			date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
			return date;
		}else if(FORMATYYYYMMDDHHMMSS.equals(format)||FORMATYYYYMMDDHHMMSS_Z.equals(format)){
			time = convertString(time);
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
			return date;
		}
		throw new Exception(FORMATUNCR);
	}
	
	public String convertString(String time){
//		if(time.contains("/")){
//			time = time.replace("/", "-");
//		}else if(time.contains(".")){
//			time = time.replace(".", "-");
//		}else {
//			
//		}
		String format = judgeFormat(time);
		if(FORMATYYYYMMMDD.equals(format)||FORMATYYYYMMDDHHMMSS.equals(format)){
			time = time.replaceAll("/|\\.", "-");
		}else if(FORMATYYYYMMDD_Z.equals(format)){
			time = time.replaceAll("[\u4e00-\u9fa5]", "-");
			time = time.substring(0, time.length() - 1);
		}else if(FORMATYYYYMMDDHHMMSS_Z.equals(format)){
			time = time.replaceAll("[\u4e00-\u9fa5]", "-");
			String[] timeArray = time.split("-");
			time = timeArray[0] + "-" + timeArray[1] + "-" + timeArray[2] + " " + timeArray[3] + ":" + timeArray[4] +":"+ timeArray[5];
		}
		return time;
	}
----------------------------------------------------------------------------------------------------------- 之前寫的判斷兩次格式有點浪費啊,修改converString方法
public static final String FORMATYYYYMMMDD = "yyyy-MM-dd||yyyy.MM.dd||yyyy/MM/dd";
	public static final String FORMATYYYYMMDD_Z = "yyyy年MM月dd日";
	public static final String FORMATYYYYMMDDHHMMSS = "yyyy.MM.dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||yyyy-MM-dd HH:mm:ss";
	public static final String FORMATYYYYMMDDHHMMSS_Z = "yyyy年MM月dd日 HH時mm分ss秒";
	public static final String FORMATUNCR = "格式不符合要求";
	public String judgeFormat(String time){
//		String regex1 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}$";
//		String regex2 = "^\\d{4}\\S{1}\\d{2}\\S{1}\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}$";
		String regex1 = "^\\s*\\d{4}(-|/|\\.){1}\\d{2}(-|/|\\.){1}\\d{2}\\s*";
		String regex2 = "^\\s*\\d{4}(-|/|\\.){1}\\d{2}(-|/|\\.){1}\\d{2}\\s+(\\d{2}:){2}\\d{2}\\s*";
		String regex3 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}\\d{2}([\u4e00-\u9fa5]){1}\\d{2}([\u4e00-\u9fa5]){1}\\s*";
		String regex4 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}(\\d{2}([\u4e00-\u9fa5]){1}){2}\\s*(\\d{2}([\u4e00-\u9fa5]){1}){3}\\s*";
		Pattern p1 = Pattern.compile(regex1);
		Matcher m1 = p1.matcher(time);
		if(m1.matches()){
			return FORMATYYYYMMMDD;
		}
		Pattern p2 = Pattern.compile(regex2);
		Matcher m2 = p2.matcher(time);
		if(m2.matches()){
			return FORMATYYYYMMDDHHMMSS;
		}
		Pattern p3 = Pattern.compile(regex3);
		Matcher m3 = p3.matcher(time);
		if(m3.matches()){
			return FORMATYYYYMMDD_Z;
		}
		Pattern p4 = Pattern.compile(regex4);
		Matcher m4 = p4.matcher(time);
		if(m4.matches()){
			return FORMATYYYYMMDDHHMMSS_Z;
		}
		return FORMATUNCR;
	}
	
	public Date convertStringtoDate(String time) throws Exception{
		Date date = null;
		String format = judgeFormat(time);
		if(FORMATYYYYMMMDD.equals(format)||FORMATYYYYMMDD_Z.equals(format)){
			time = convertString(format,time);
			date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
			return date;
		}else if(FORMATYYYYMMDDHHMMSS.equals(format)||FORMATYYYYMMDDHHMMSS_Z.equals(format)){
			time = convertString(format,time);
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
			return date;
		}
		throw new Exception(FORMATUNCR);
	}
	
	public String convertString(String format,String time){
//		if(time.contains("/")){
//			time = time.replace("/", "-");
//		}else if(time.contains(".")){
//			time = time.replace(".", "-");
//		}else {
//			
//		}
//		String format = judgeFormat(time);
		if(FORMATYYYYMMMDD.equals(format)||FORMATYYYYMMDDHHMMSS.equals(format)){
			time = time.replaceAll("/|\\.", "-");
		}else if(FORMATYYYYMMDD_Z.equals(format)){
			time = time.replaceAll("[\u4e00-\u9fa5]", "-");
			time = time.substring(0, time.length() - 1);
		}else if(FORMATYYYYMMDDHHMMSS_Z.equals(format)){
			time = time.replaceAll("[\u4e00-\u9fa5]", "-");
			String[] timeArray = time.split("-");
			time = timeArray[0] + "-" + timeArray[1] + "-" + timeArray[2] + " " + timeArray[3] + ":" + timeArray[4] +":"+ timeArray[5];
		}
		return time;
	}