/**
* 判断是不是合法手机
* handset 手机号码
*/
public static boolean isHandset(String handset) {
try {
if(!handset.substring(0,1).equals("1")){
return false;
}
if (handset==null || handset.length()!=11) {
return false;
}
String check ="^[0123456789]+$";
Pattern regex =Pattern.compile(check);
Matcher matcher =regex.matcher(handset);
boolean isMatched = matcher.matches();
if(isMatched) {
return true;
} else {
return false;
}
} catch (RuntimeException e) {
return false;
}
}
}
/**
* 判断是不是合法Email
* email Email地址
*/
public static boolean isEmail(String email) {
try {
if (email==null || email.length()<1 || email.length()>256){
return false;
}
String check ="^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
Pattern regex =Pattern.compile(check);
Matcher matcher =regex.matcher(email);
boolean isMatched = matcher.matches();
if(isMatched) {
return true;
} else {
return false;
}
} catch (RuntimeException e) {
return false;
}
}
importjava.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 序列化对象为JSON格式遵循JSON组织公布标准
*
* @date 2008/05/07
* @version 1.0.0
*/
public class Json {
/**Commons Logging instance. */
private static org.apache.commons.logging.Log log =org.apache.commons.logging.LogFactory.getLog(Json.class);
/**
* @param obj 任意对象
* @return String
*/
public static String object2json(Object obj) {
StringBuilder json = newStringBuilder();
if (obj == null) {
json.append("/"/"");
} else if (obj instanceof String || obj instanceof Integer || obj instanceof Float || obj instanceofBoolean
|| obj instanceof Short || obj instanceof Double || obj instanceof Long || obj instanceofBigDecimal
|| obj instanceof BigInteger ||obj instanceofByte) {
json.append("/"").append(string2json(obj.toString())).append("/"");
} else if (obj instanceof Object[]){
json.append(array2json((Object[]) obj));
} else if (obj instanceof List){
json.append(list2json((List<?>) obj));
} else if (obj instanceof Map){
json.append(map2json((Map<?, ?>) obj));
} else if (obj instanceof Set){
json.append(set2json((Set<?>) obj));
} else {
json.append(bean2json(obj));
}
returnjson.toString();
}
/**
* @param bean bean对象
* @return String
*/
public static String bean2json(Object bean) {
StringBuilder json = newStringBuilder();
json.append("{");
PropertyDescriptor[] props = null;
try {
props =Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();
} catch(IntrospectionException e) {
}
if (props != null) {
for (int i = 0; i <props.length; i++) {
try {
String name =object2json(props[i].getName());
String value =object2json(props[i].getReadMethod().invoke(bean));
json.append(name);
json.append(":");
json.append(value);
json.append(",");
} catch (Exception e){
}
}
json.setCharAt(json.length() - 1, '}');
} else {
json.append("}");
}
returnjson.toString();
}
/**
* @param list list对象
* @return String
*/
public static String list2json(List<?> list) {
StringBuilder json = newStringBuilder();
json.append("[");
if (list != null && list.size()> 0) {
for (Object obj : list){
json.append(object2json(obj));
json.append(",");
}
json.setCharAt(json.length() - 1, ']');
} else {
json.append("]");
}
returnjson.toString();
}
/**
* @param array 对象数组
* @return String
*/
public static String array2json(Object[] array) {
StringBuilder json = newStringBuilder();
json.append("[");
if (array != null &&array.length > 0) {
for (Object obj : array){
json.append(object2json(obj));
json.append(",");
}
json.setCharAt(json.length() - 1, ']');
} else {
json.append("]");
}
returnjson.toString();
}
/**
* @param map map对象
* @return String
*/
public static String map2json(Map<?, ?> map) {
StringBuilder json = newStringBuilder();
json.append("{");
if (map != null && map.size()> 0) {
for (Object key :map.keySet()) {
json.append(object2json(key));
json.append(":");
json.append(object2json(map.get(key)));
json.append(",");
}
json.setCharAt(json.length() - 1, '}');
} else {
json.append("}");
}
returnjson.toString();
}
/**
* @param set 集合对象
* @return String
*/
public static String set2json(Set<?> set) {
StringBuilder json = newStringBuilder();
json.append("[");
if (set != null && set.size()> 0) {
for (Object obj : set){
json.append(object2json(obj));
json.append(",");
}
json.setCharAt(json.length() - 1, ']');
} else {
json.append("]");
}
returnjson.toString();
}
/**
* @param s 参数
* @return String
*/
public static String string2json(String s) {
if (s == null)
return"";
StringBuilder sb = newStringBuilder();
for (int i = 0; i <s.length(); i++) {
char ch =s.charAt(i);
switch (ch) {
case '"':
sb.append("///"");
break;
case '//':
sb.append("");
break;
case '/b':
sb.append("//b");
break;
case '/f':
sb.append("//f");
break;
case '/n':
sb.append("//n");
break;
case '/r':
sb.append("//r");
break;
case '/t':
sb.append("//t");
break;
case '/':
sb.append("///");
break;
default:
if (ch >= '/u0000'&& ch <= '/u001F') {
String ss =Integer.toHexString(ch);
sb.append("//u");
for (int k = 0; k < 4 -ss.length(); k++) {
sb.append('0');
}
sb.append(ss.toUpperCase());
} else {
sb.append(ch);
}
}
}
returnsb.toString();
}
}
/**
* Big5字与Unicode的互换
* 转换后的正常字型
*/
importjava.io.*;
public class MyUtil{
public static String big5ToUnicode(String s){
try{
return newString(s.getBytes("ISO8859_1"), "Big5");
}
catch (UnsupportedEncodingException uee){
return s;
}
}
public static String UnicodeTobig5(String s){
try{
return newString(s.getBytes("Big5"), "ISO8859_1");
}
catch (UnsupportedEncodingException uee){
return s;
}
}
public static String toHexString(String s){
Stringstr="";
for (int i=0; i<s.length(); i++){
int ch=(int)s.charAt(i);
Strings4="0000"+Integer.toHexString(ch);
str=str+s4.substring(s4.length()-4)+" ";
}
return str;
}
}
/**
* 人民币转成大写
*
* @param value
* @return String
*/
public static String hangeToBig(double value)
{
char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示
char[] vunit = { '万', '亿'}; // 段名表示
char[] digit = { '零', '壹','贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; //数字表示
long midVal = (long) (value * 100); // 转化成整形
String valStr = String.valueOf(midVal); //转化成字符串
String head = valStr.substring(0,valStr.length() - 2); // 取整数部分
String rail = valStr.substring(valStr.length() - 2); // 取小数部分
String prefix = ""; // 整数部分转化的结果
String suffix = ""; //小数部分转化的结果
// 处理小数点后面的数
if(rail.equals("00"))
{ //如果小数部分为0
suffix = "整";
}
else
{
suffix = digit[rail.charAt(0)- '0'] + "角" + digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来
}
//处理小数点前面的数
char[] chDig =head.toCharArray(); // 把整数部分转化成字符数组
char zero = '0'; // 标志'0'表示出现过0
byte zeroSerNum = 0; // 连续出现0的次数
for (int i = 0; i <chDig.length; i++)
{ //循环处理每个数字
int idx= (chDig.length - i - 1) % 4; // 取段内位置
intvidx = (chDig.length - i - 1) / 4; //取段位置
if(chDig[i] == '0')
{ // 如果当前字符是0
zeroSerNum++; // 连续0次数递增
if(zero == '0')
{ // 标志
zero = digit[0];
}
else if (idx == 0 &&vidx > 0 && zeroSerNum < 4)
{
prefix += vunit[vidx- 1];
zero ='0';
}
continue;
}
zeroSerNum = 0; // 连续0次数清零
if(zero != '0')
{ // 如果标志不为0,则加上,例如万,亿什么的
prefix += zero;
zero = '0';
}
prefix += digit[chDig[i] -'0']; // 转化该数字表示
if (idx> 0)
prefix += hunit[idx -1];
if (idx == 0 &&vidx > 0)
{
prefix += vunit[vidx -1]; // 段结束位置应该加上段名如万,亿
}
}
if (prefix.length() > 0)
prefix += '圆'; // 如果整数部分存在,则有圆的字样
return prefix + suffix; // 返回正确表示
}
/**
* 全角字符转半角字符
*
* @param QJStr
* @return String
*/
public static final String QJToBJChange(String QJStr)
{
char[] chr = QJStr.toCharArray();
String str ="";
for (int i = 0; i < chr.length; i++)
{
chr[i] = (char) ((int) chr[i] -65248);
str += chr[i];
}
return str;
}
/**
* 判断输入的字符串是否符合Email样式.
*
* @param str 传入的字符串
* @return 是Email样式返回true,否则返回false
*/
public static boolean isEmail(String str) {
Pattern pattern =Pattern.compile("^//w+([-+.]//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*$");
return pattern.matcher(str).matches();
}
/**
* 判断输入的字符串是否为纯汉字
*
* @param str 传入的字符窜
* @return 如果是纯汉字返回true,否则返回false
*/
public static boolean isChinese(String str) {
Pattern pattern =Pattern.compile("[/u0391-/uFFE5]+$");
return pattern.matcher(str).matches();
}
/**
* 是否为空白,包括null和""
*
* @param str
* @return
*/
public static boolean isBlank(String str) {
return str == null || str.trim().length() == 0;
}
/**
* 获取网卡MAC地址
*/
public static String getMacOnWindow() {
try {
String mac = null;
Process process =Runtime.getRuntime().exec("ipconfig /all");
BufferedReader buffer =
new BufferedReader(newInputStreamReader(process.getInputStream()));
for (String line = buffer.readLine(); line!= null; line = buffer.readLine()) {
int index = line.indexOf("PhysicalAddress");
if (index <= 0) {
continue;
}
mac = line.substring(index +36);
break;
}
buffer.close();
process.waitFor();
return mac;
} catch (Exception exception) {
return null;
}
}