1. 程式人生 > >java中的一些實用工具

java中的一些實用工具

/**
	 * 獲取請求IP地址
	 * @param request
	 * @return
	 */
	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        final String[] arr = ip.split(",");  
        for (final String str : arr) {  
            if (!"unknown".equalsIgnoreCase(str)) {  
                ip = str;  
                break;  
            }  
        }  
        return ip;  
	}
/**
     * 通過身份證號碼獲取性別、年齡、星座
     * @param certificateNo
     * @return
     */
    public static JSONObject getBirAgeSex(String certificateNo) {
    	if(StringUtils.isEmpty(certificateNo)) {
    		 JSONObject j = new JSONObject();
	        j.put("birthday", "");
	        j.put("age", "");
	        j.put("sexCode", "");
	        j.put("star", "");
	        return j;
    	}
        String birthday = "";
        String age = "";
        String sexCode = "";
        int year = Calendar.getInstance().get(Calendar.YEAR);
        birthday = certificateNo.substring(6, 10) + "-"
                + certificateNo.substring(10, 12) + "-"
                + certificateNo.substring(12, 14);
        sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1)) % 2 == 0 ? "F" : "M";
        age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
        int month = Short.valueOf(certificateNo.substring(10, 12));
	    int day = Short.valueOf(certificateNo.substring(12, 14));
	    String strValue = "";
	    if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
	    	strValue = "水瓶座";
	    } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
	    	strValue = "雙魚座";
	    } else if ((month == 3 && day > 20) || (month == 4 && day <= 19)) {
	    	strValue = "白羊座";
	    } else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
	    	strValue = "金牛座";
	    } else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
	    	strValue = "雙子座";
	    } else if ((month == 6 && day > 21) || (month == 7 && day <= 22)) {
	    	strValue = "巨蟹座";
	    } else if ((month == 7 && day > 22) || (month == 8 && day <= 22)) {
	    	strValue = "獅子座";
	    } else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
	    	strValue = "處女座";
	    } else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
	    	strValue = "天秤座";
	    } else if ((month == 10 && day > 23) || (month == 11 && day <= 22)) {
	    	strValue = "天蠍座";
	    } else if ((month == 11 && day > 22) || (month == 12 && day <= 21)) {
	    	strValue = "射手座";
	    } else if ((month == 12 && day > 21) || (month == 1 && day <= 19)) {
	    	strValue = "魔羯座";
	    }
	    JSONObject j = new JSONObject();
        j.put("birthday", birthday);
        j.put("age", age);
        j.put("sexCode", sexCode);
        j.put("star", strValue);
        return j;
    }
/**
	 * 電話號碼校驗
	 * @param phone
	 * @return
	 */
	public static boolean validPhone(String phone ) {
		if(!StringUtils.isEmpty(phone)) {
			Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9])|(19[8-9])|(166)|(14[0-9]))\\d{8}$");
			Matcher m = p.matcher(phone);
			return m.matches();
		}else {
			return false;
		}
	}