⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.java

📁 用java开发的一些实用的短信通信模块其中包含MD5加密、http发送等信息
💻 JAVA
字号:
package lib.commons;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;

import lib.commons.logging.LogFactory;

public final class Utils {
	public static final String EMPTY_STRING = StringUtils.EMPTY;

	public static final String md5Hex(String data) {
		return DigestUtils.md5Hex(data);
	}

	public static final Log getLog() {
		return LogFactory.getLog();
	}

	public static final Log getLog(Class clazz) {
		return LogFactory.getLog(clazz);
	}

	public static final Log getLog(String logName) {
		return LogFactory.getLog(logName);
	}

	public static final boolean StringIsNullOrEmpty(String str) {
		return null == str || str.length() == 0;
	}

	public static final boolean TrimStringIsNullOrEmpty(String str) {
		return null == str || str.trim().length() == 0;
	}

	public static final Pattern IPv4_Pattern = Pattern
			.compile("\\d{1,4}\\.\\d{1,4}\\.\\d{1,4}\\.\\d{1,4}");

	public static final boolean isIPv4Address(String address) {
		if (!StringIsNullOrEmpty(address)
				&& IPv4_Pattern.matcher(address).matches())
			return true;
		else
			return false;
	}

	public static final byte intToByte(int i) {
		if ((i >= 0 && i < 127) || (i < 0 && i > -128))
			return (byte) i;
		else if (i > 127 && i < 255)
			return (byte) (i - 256);
		else
			return -1;
	}

	public static final int byteToUint(byte b) {
		if (b >= 0)
			return b;
		else
			return 256 + b;
	}

	public static final byte[] intToBytes(int i) {
		return intToBytes(i, java.nio.ByteOrder.BIG_ENDIAN);
	}

	public static final byte[] intToBytes(int i, java.nio.ByteOrder byteOrder) {
		byte[] bytes = new byte[4];
		if (byteOrder == java.nio.ByteOrder.LITTLE_ENDIAN) {
			for (int j = 0; j < 4; j++) {
				bytes[j] = (byte) (i >> j * 8);
			}
		} else {
			for (int j = 0; j < 4; j++) {
				bytes[j] = (byte) (i >> 24 - j * 8);
			}
		}
		return bytes;
	}

	public static final int bytesToInt(byte[] bytes) {
		return bytesToInt(bytes, java.nio.ByteOrder.BIG_ENDIAN);
	}

	public static final int bytesToInt(byte[] bytes,
			java.nio.ByteOrder byteOrder) {
		int n = 0;
		if (null != bytes && bytes.length > 0) {
			if (byteOrder == java.nio.ByteOrder.LITTLE_ENDIAN) {
				for (int i = 0; i < bytes.length && i < 4; i++) {
					n += (byteToUint(bytes[i])) << i * 8;
				}
			} else {
				int len = bytes.length > 4 ? 4 : bytes.length;
				for (int i = 0; i < bytes.length && i < 4; i++) {
					n += (byteToUint(bytes[i])) << (8 * (len - 1 - i));
				}
			}
		}
		return n;
	}

	public static final int getRandomInt(int size) {
		int r = 0;
		if (size > 1) {
			java.util.Random random = new java.util.Random();
			for (int i = 0; i < 2; i++) {
				r = (random.nextInt() >>> 1) % size;
			}
		}
		return r < 0 ? 0 : r;
	}
	
	public static final long getRandomLong(long size) {
		long r = 0;
		if (size > 1) {
			java.util.Random random = new java.util.Random();
			for (int i = 0; i < 2; i++) {
				r = (random.nextLong() >>> 1) % size;
			}
		}
		return r < 0 ? 0 : r;
	}
	
	public static String dateToString(java.util.Date date,String format){
		String str = org.apache.commons.lang.time.DateFormatUtils.format(date, format);
		return str;
	}
	
	public static java.util.Date stringToDate(String dateStr,String format) throws Exception{
		java.util.Date date = org.apache.commons.lang.time.DateUtils.parseDate(dateStr, new String[]{format});
		return date;
	}

	public static void main(String[] args) throws Exception{
		/*
		 * int n = 80; byte[] bytes = intToBytes(n,
		 * java.nio.ByteOrder.BIG_ENDIAN); for (int i = 0; i < bytes.length;
		 *i++) { System.out.println(bytes[i]); } bytes = new byte[] { 0, 80 };
		 *System.out.println(bytesToInt(bytes, java.nio.ByteOrder.BIG_ENDIAN));
		 */
		//System.out.println(dateToString(stringToDate("20061105123015","yyyyMMddHHmmss"),"yyyy-MM-dd HH:mm:ss"));
		lib.commons.util.ThreadControlHelper.threadWaitMillSeconds(new TestThread(), 20000, 100);
		for (int i=0;i<1000;i++){
			System.out.println(getRandomLong(180000));
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -