Reconstruction of the actual code that (1) the date converting method Conversion

Reconstruction of the code conversion 1- actual date converting method

Foreword

This code is responsible for the project out to me, this method is a framework of basic class methods. The main role is: to turn the string into a date .

Reconstruction of the former Code

public static java.util.Date formatDate(String strValue) throws ParseException {
		Pattern pattern1 = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}");
		Pattern pattern2 = Pattern.compile("^\\d{4}\\d{2}\\d{2}\\s+\\d{2}\\d{2}\\d{2}");
		Pattern pattern3 = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}");
		Pattern pattern4 = Pattern.compile("^\\d{4}\\d{2}\\d{2}");
        Pattern pattern41 = Pattern.compile("^\\d{4}-\\d{1}-\\d{1}");
        Pattern pattern42 = Pattern.compile("^\\d{4}-\\d{1}-\\d{2}");
        Pattern pattern43 = Pattern.compile("^\\d{4}-\\d{2}-\\d{1}");
		Pattern pattern5 = Pattern.compile("^\\d{4}");
		Pattern pattern6 = Pattern.compile("^\\d{4}/\\d{2}/\\d{2}");
		Pattern pattern7 = Pattern.compile("^\\d{4}年\\d{2}月\\d{2}日");
		Pattern pattern8 = Pattern.compile("^\\d{4}年\\d{1}月\\d{1}日");
		Pattern pattern9 = Pattern.compile("^\\d{4}年\\d{1}月\\d{2}日");
		Pattern pattern10 = Pattern.compile("^\\d{4}年\\d{2}月\\d{1}日");
		Matcher matcher1 = pattern1.matcher(strValue);
		Matcher matcher2 = pattern2.matcher(strValue);
		Matcher matcher3 = pattern3.matcher(strValue);
		Matcher matcher4 = pattern4.matcher(strValue);
		Matcher matcher5 = pattern5.matcher(strValue);
		try {
			SimpleDateFormat datePaser;
			if (matcher1.matches()) {
				datePaser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return datePaser.parse(strValue);
			}
			if (matcher2.matches()) {
				datePaser = new SimpleDateFormat("yyyyMMdd HHmmss");
				return datePaser.parse(strValue);
			}
			if (matcher3.matches()) {
				datePaser = new SimpleDateFormat("yyyy-MM-dd");
				return datePaser.parse(strValue);
			}
			if (matcher4.matches()) {
				datePaser = new SimpleDateFormat("yyyyMMdd");
				return datePaser.parse(strValue);
			}
            if (pattern41.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy-M-d");
                return datePaser.parse(strValue);
            }
            if (pattern42.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy-M-dd");
                return datePaser.parse(strValue);
            }
            if (pattern42.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy-MM-d");
                return datePaser.parse(strValue);
            }

			if (matcher5.matches()) {
				datePaser = new SimpleDateFormat("yyyy");
				return datePaser.parse(strValue);
			}
            if (pattern43.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy-M-dd");
                return datePaser.parse(strValue);
            }

			if (pattern6.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy/MM/dd");
                return datePaser.parse(strValue);
            }
			if (pattern7.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy年MM月dd日");
                return datePaser.parse(strValue);
            }
			if (pattern8.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy年M月d日");
                return datePaser.parse(strValue);
            }
			if (pattern9.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy年M月dd日");
                return datePaser.parse(strValue);
            }
			if (pattern10.matcher(strValue).matches()) {
                datePaser = new SimpleDateFormat("yyyy年MM月d日");
                return datePaser.parse(strValue);
            }
			throw new ParseException(strValue + "不符合date转换格式", 0);
		} catch (ParseException e1) {
			throw new ParseException(strValue + "date转换异常", 0);
		}
	}

Code issues

1: multi-variable, the variable name is not standardized.
2: defining and using variable spacing too far, readability bad.
3: writing specifications are not unified, a method retains a variety of styles.
4: Regular problems check sequence, using the non-regular closed (with starting symbol, no terminator).
5: code duplication.
6: remove unreasonable code, such as "yyyy" conversion. Feeling mass chaos can turn into a date, leave the seeds. . .

Improved version

    /**
     * 字符串转成util.Date
     * 支持字符串格式:yyy-MM-dd HH:mm:ss,yyyyMMdd HHmmss,yyyy-MM-dd,yyyyMMdd,yyyy,yyyy/MM/dd,yyyy年MM月dd日,yyyy年M月d日,yyyy年M月dd日,yyyy年MM月d日
     */
    public static java.util.Date formatDate(String strValue) throws ParseException {
        Map<String,String> mRgx = getFixMap();
        for ( Map.Entry<String,String> entry : mRgx.entrySet()) {
            if (Pattern.compile(entry.getValue()).matcher(strValue).matches()) {
                    return new SimpleDateFormat(entry.getKey()).parse(strValue);
            }
        }
        throw new ParseException(strValue + "不符合date转换格式", 0);
    }

    private static Map<String,String> fixMap;
    private static Map getFixMap() {
        if (fixMap == null) {
            fixMap = new HashMap();
            fixMap.put("yyyy-MM-dd HH:mm:ss", "^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}$");
            fixMap.put("yyyy-MM-dd", "^\\d{4}-\\d{1,2}-\\d{1,2}$");
            fixMap.put("yyyyMMdd", "^\\d{4}\\d{2}\\d{2}$");
            fixMap.put("yyyy/MM/dd", "^\\d{4}/\\d{1,2}/\\d{1,2}$");
            fixMap.put("yyyy年MM月dd日", "^\\d{4}年\\d{1,2}月\\d{1,2}日$");
        }
        return fixMap;
    }

PS

1: always remove some do not have access or can not use, such as "yyyy", free with easy mistake is a fine line between.
2: The combined some of which may combined, such as regular expression "^ \ d {4} - \ d {1} - \ d {1}", "^ \ d {4} - \ d {1} - \ d { 2} "," ^ \ d {4} - \ d {2} - \ d {1} "," ^ \ d {4} - \ d {2} - \ d {2} ". It may be combined into "^ \. 4 {D} - \ D {1,2} - \ D {1,2}"
. 3: To further facilitate the expansion (although nothing extended), the introduction of the Map..
4: Increase regular expression terminator, rigorous matching.

Published 21 original articles · won praise 47 · views 3921

Guess you like

Origin blog.csdn.net/richyliu44/article/details/104808665