郵件和簡訊傳送

太陽以西.S發表於2020-10-13

簡訊傳送

  • 使用阿里雲介面。
public class Sms {
    public static void main(String[] args) {
        sendMsg("寫手機號","模板json","模板json");

    }

    public static void sendMsg(String phone,String name,String days){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKey ID", "AccessKey Secret");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "模板名稱");
        request.putQueryParameter("TemplateCode", "SMS_199580007");
        request.putQueryParameter("TemplateParam", "{name:"+name+",days:"+days+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

郵件傳送

  • 依賴:mail.jar
  • 發信郵箱需開啟POP3/SMTP服務,並獲得授權碼。

手寫測試

public class SendEmail {
    public static void main(String[] args) throws Exception {
        String from = "發信郵箱";
        String upwd = "授權碼";
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 連線協議
        properties.put("mail.smtp.host", "smtp.qq.com");// 主機名 smtp.163.com
        properties.put("mail.smtp.port", 465);// 埠號 465安全連線
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");// 設定是否使用ssl安全連線 ---一般都使用
        properties.put("mail.debug", "true");// 設定是否顯示debug資訊 true 會在控制檯顯示相關資訊

        //會話物件
        Session session = Session.getDefaultInstance(properties);

        Message message = new MimeMessage(session);
        message.setSubject("新年快樂");//主題
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("收件郵箱,可以是陣列"));
        message.setText("祝大家新年快樂,假期愉快!");

        //郵差物件
        Transport transport = session.getTransport();
        transport.connect(from,upwd);
        transport.sendMessage(message,message.getAllRecipients());
    }
}

使用封裝的工具類

  • EmailTest.java
public class EmailTest {
	
	public static void main(String[] args) throws Exception
	{
		try
		{
			MailSender mail = new MailSender();
			
			mail.setHost("smtp.qq.com");			// 設定 SMTP 主機
			mail.setFrom("發件郵箱");		// 設定傳送者地址xxx@163.com
			mail.addTo("目標地址");				// 新增傳送目標地址xxx@xx.com
			//mail.addCc("抄送的郵箱地址");				// 新增抄送目標地址xxx@xx.com
			mail.addFileAcc("附件");			// 新增檔案附件
			// 從 byte[] 中讀取資料並新增為附件(這個功能有時非常有用)
			mail.addByteAcc("我是大怪獸".getBytes(), MailSender.DEFAULT_CONTENT_TYPE, "我是誰.txt");
			//mail.addReplyTo(mail.getFrom());		// 新增回復地址
			mail.setAuth(true);						// 設定驗證模式
//			mail.setNeedReceipt(true);				// 設定是否需要回執
			mail.setUser("發件郵箱");			// 設定郵箱登入名
			mail.setPassword("授權碼");			// 設定郵箱登入密碼
			mail.setSubject("郵件主題");				// 設定郵件主題
			mail.setText("<h1>您好,點選一下連結進行郵箱驗證:</h1>測試郵件<br>這是一封java發出的郵件");// 設定郵件文字內容
			mail.setCharset("UTF-8");				// 設定郵件文字內容編碼
			mail.setContentType("text/html");		// 設定郵件文字內容格式
			
			mail.send();							// 傳送郵件
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
  • MailSender.java
/**
 * ClassName: MailSender 
 * @Description: 郵件傳送
 * @author Mr.Wang
 * @date 2017年11月11日
 */
public class MailSender
{
	/** 預設 Content Type -> text/plain */
	public static final String DEFAULT_CONTENT_TYPE	= "text/plain";
	/** 預設字元編碼 -> UTF-8 */
	public static final String DEFAULT_ENCODING		= GeneralHelper.DEFAULT_ENCODING;
	/** 預設 SMTP 埠 -> 25 */
	public static final int DEFAULT_PORT			= 465;
	
	private String host			= "";
	private String from			= "";
	private String user			= "";
	private String password		= "";
	private String subject		= "";
	private String text			= "";
	private String contentType	= DEFAULT_CONTENT_TYPE;
	private String charset		= DEFAULT_ENCODING;
	private int port			= DEFAULT_PORT;
	private boolean auth		= true;
	private boolean needReceipt	= false;
	private Date sentDate		= null;
	
	private List<String>		to				= new ArrayList<String>();
	private List<String>		cc				= new ArrayList<String>();
	private List<String>		bcc				= new ArrayList<String>();
	private List<String>		replyTo			= new ArrayList<String>();
	private List<String> 		fileAcc 		= new ArrayList<String>();
	private List<MimeBodyPart> 	byteAcc 		= new ArrayList<MimeBodyPart>();

	public int getPort()
	{
		return port;
	}

	public void setPort(int port)
	{
		this.port = port;
	}
	
	public boolean isAuth()
	{
		return auth;
	}

	public void setAuth(boolean auth)
	{
		this.auth = auth;
	}

	public String getCharset()
	{
		return charset;
	}

	public void setCharset(String charset)
	{
		this.charset = charset;
	}

	public String getContentType()
	{
		return contentType;
	}

	public void setContentType(String contentType)
	{
		this.contentType = contentType;
	}

	public boolean isNeedReceipt()
	{
		return needReceipt;
	}

	public void setNeedReceipt(boolean needReceipt)
	{
		this.needReceipt = needReceipt;
	}

	public String getFrom()
	{
		return from;
	}

	public void setFrom(String from)
	{
		this.from = from;
	}

	public String getHost()
	{
		return host;
	}

	public void setHost(String host)
	{
		this.host = host;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getSubject()
	{
		return subject;
	}

	public void setSubject(String subject)
	{
		this.subject = subject;
	}

	public String getText()
	{
		return text;
	}

	public void setText(String text)
	{
		this.text = text;
	}
	
	public Date getSentDate()
	{
		return sentDate;
	}
	
	public void setSentDate(Date sentDate)
	{
		this.sentDate = sentDate;
	}

	public String getUser()
	{
		return user;
	}

	public void setUser(String user)
	{
		this.user = user;
	}

	public List<String> getFileAcc()
	{
		return fileAcc;
	}
	
	public List<MimeBodyPart> getByteAcc()
	{
		return byteAcc;
	}
	
	public void setFileAcc(List<String> accessory)
	{
		this.fileAcc = accessory;
	}
	
	public void setByteAcc(List<MimeBodyPart> accessory)
	{
		this.byteAcc = accessory;
	}

	public List<String> getReplyTo()
	{
		return replyTo;
	}

	public List<String> getTo()
	{
		return to;
	}
	
	public void setTo(List<String> to)
	{
		this.to = to;
	}
	
	public List<String> getCc()
	{
		return cc;
	}
	
	public void setCc(List<String> cc)
	{
		this.cc = cc;
	}
	
	public List<String> getBcc()
	{
		return bcc;
	}
	
	public void setBcc(List<String> bcc)
	{
		this.bcc = bcc;
	}
	
	public void addFileAcc(String accessory)
	{
		fileAcc.add(accessory);
	}
	
	/** 新增 byte array 形式的附件 */
	public void addByteAcc(byte[] accessory, String type, String fileName) throws Exception
	{
		ByteArrayDataSource ds	 = new ByteArrayDataSource(accessory, type, fileName);

		//BASE64Encoder enc = new BASE64Encoder();
		//fileName = "=?GBK?B?" + enc.encode(fileName.getBytes()) + "?=";
		fileName = MimeUtility.encodeText(fileName, charset, "B");

		MimeBodyPart mimeFile = new MimeBodyPart();
		mimeFile.setDataHandler(new DataHandler(ds));
		//mimeFile.setFileName(ds.getName());
		mimeFile.setFileName(fileName);
		
		byteAcc.add(mimeFile);
	}
	
	public void addReplyTo(String address)
	{
		replyTo.add(address);
	}
	
	public void addTo(String address)
	{
		to.add(address);
	}
	
	public void addCc(String address)
	{
		cc.add(address);
	}
	
	public void addBcc(String address)
	{
		bcc.add(address);
	}
	
	/** 傳送郵件 */
	public void send() throws Exception
	{
		Transport transport = null;
		
		try
		{
			Properties props = new Properties();
			props.put("mail.transport.protocol", "smtp");
			props.put("mail.smtp.auth", Boolean.toString(auth));
			props.put("mail.smtp.ssl.enable", "true");
			Session session = Session.getDefaultInstance(props, null);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			
			for(String i : to)
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(i));
			for(String i : cc)
				msg.addRecipient(Message.RecipientType.CC, new InternetAddress(i));
			for(String i : bcc)
				msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(i));

			if(replyTo.size() > 0)
			{
				InternetAddress[] replyAddress = new InternetAddress[replyTo.size()];
				
				for(int i = 0; i < replyAddress.length; i++)
					replyAddress[i] = new InternetAddress((String)replyTo.get(i));
				
				msg.setReplyTo(replyAddress);
			}
			
			if(needReceipt)
				msg.addHeader("Disposition-Notification-To", from);
			
			if(sentDate != null)
				msg.setSentDate(sentDate);
			else
				msg.setSentDate(new Date());
			
			msg.setSubject(subject, charset);
			
			MimeMultipart mm	= new MimeMultipart();
			MimeBodyPart mbText	= new MimeBodyPart();
			mbText.setContent(text, contentType + ";charset=" + charset);
			mm.addBodyPart(mbText);
			
			for(String filePath : fileAcc)
			{
				String fileName = (new File(filePath)).getName();
				fileName = MimeUtility.encodeText(fileName, charset, "B");
				
				MimeBodyPart mbFile		= new MimeBodyPart();
				DataSource datasource	= new FileDataSource(filePath);
				
				mbFile.setDataHandler(new DataHandler(datasource));
				mbFile.setFileName(fileName);
				mm.addBodyPart(mbFile);
			}
			
			for(MimeBodyPart part : byteAcc)
				mm.addBodyPart(part);
			
			msg.setContent(mm);
			msg.saveChanges();
			
			transport = session.getTransport();
			transport.connect(host, port, user, password);
			transport.sendMessage(msg, msg.getAllRecipients());
		}
		finally
		{
			if(transport != null) try{ transport.close(); } catch (Exception e) { }
		}
	}
}
  • ByteArrayDataSource .java
/** Byte Array 資料來源,傳送位元組陣列附件時用到 */
class ByteArrayDataSource implements DataSource
{
	public static final String DEFAULT_ENCODING	= GeneralHelper.DEFAULT_ENCODING;
	
	private ByteArrayOutputStream	baos		= null;
	private String					type		= "application/octet-stream";
	private String					name		= "ByteArrayDataSource";
	
	private void init(String type, String name)
	{
		if(type != null)
			this.type = type;
		if(name != null)
			this.name = name;		
	}

	public ByteArrayDataSource(byte[] data, String type, String name) throws IOException
	{
		ByteArrayInputStream Bis = null;

		try
		{
			Bis = new ByteArrayInputStream(data);
			this.byteArrayDataSource(Bis, type, name);
		}
		catch (IOException ioex)
		{
			throw ioex;
		}
		finally
		{
			try
			{
				if (Bis != null)
				{
					Bis.close();
				}
			}
			catch (IOException ignored)
			{
			}
		}

	}

	public ByteArrayDataSource(InputStream aIs, String type, String name) throws IOException
	{
		this.byteArrayDataSource(aIs, type, name);
	}

	private void byteArrayDataSource(InputStream aIs, String type, String name) throws IOException
	{
		init(type, name);
		
		BufferedInputStream bis		= null;
		BufferedOutputStream bos	= null;
		
		try
		{
			int length = 0;
			byte[] buffer = new byte[4096];

			bis		= new BufferedInputStream(aIs);
			baos	= new ByteArrayOutputStream();
			bos		= new BufferedOutputStream(baos);

			while ((length = bis.read(buffer)) != -1)
			{
				bos.write(buffer, 0, length);
			}		
		}
		catch (IOException ioex)
		{
			throw ioex;
		}
		finally
		{
			try
			{
				if (bis != null)
				{
					bis.close();
				}
				if (baos != null)
				{
					baos.close();
				}
				if (bos != null)
				{
					bos.close();
				}
			}
			catch (IOException ignored)
			{
			}
		}
	}

	public ByteArrayDataSource(String data, String type, String name) throws IOException
	{
		this(data, DEFAULT_ENCODING, type, name);
	}

	public ByteArrayDataSource(String data, String encoding, String type, String name) throws IOException
	{
		init(type, name);

		try
		{
			baos = new ByteArrayOutputStream();

			baos.write(data.getBytes(encoding));
		}
		catch (UnsupportedEncodingException uex)
		{
			// Do something!
		}
		catch (IOException ignored)
		{
			// Ignore
		}
		finally
		{
			try
			{
				if (baos != null)
				{
					baos.close();
				}
			}
			catch (IOException ignored)
			{
			}
		}
	}

	public String getContentType()
	{
		return type;
	}

	public InputStream getInputStream() throws IOException
	{
		if (baos == null)
		{
			throw new IOException("no data");
		}
		
		return new ByteArrayInputStream(baos.toByteArray());
	}

	public String getName()
	{
		return name;
	}

	public OutputStream getOutputStream() throws IOException
	{
		baos = new ByteArrayOutputStream();
		return baos;
	}
}
  • GeneralHelper.java
/**
 * ClassName: GeneralHelper 
 * @Description: 通用方法幫助類
 * @author Mr.Wang
 * @date 2017年11月11日
 */
public class GeneralHelper
{
	private static final String[] SHORT_DATE_PATTERN 				= {"yyyy-MM-dd", "yyyy/MM/dd", "yyyy\\MM\\dd", "yyyyMMdd"};
	private static final String[] LONG_DATE_PATTERN 				= {"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy\\MM\\dd HH:mm:ss", "yyyyMMddHHmmss"};
	private static final String[] LONG_DATE_PATTERN_WITH_MILSEC 	= {"yyyy-MM-dd HH:mm:ss.SSS", "yyyy/MM/dd HH:mm:ss.SSS", "yyyy\\MM\\dd HH:mm:ss.SSS", "yyyyMMddHHmmssSSS"};
	
	/** 空字串 */
	public static final String EMPTY_STRING				= "";
	/** 空字串 */
	public static final String DEFAULT_ENCODING			= "UTF-8";
	/** 當前作業系統平臺 */
	public static final String OS_PLATFORM				= getOSName();
	/** 當前作業系統平臺是否為 Windows */
	public static final boolean IS_WINDOWS_PLATFORM		= isWindowsPlatform();
	/** 當前作業系統平臺的換行符 */
	public static final String NEWLINE_CHAR				= IS_WINDOWS_PLATFORM ? "\r\n" : "\n";
	
	
	/** 檢查字串不為 null 或空字串 */
	public final static boolean isStrNotEmpty(String str)
	{
		return str != null && str.length() != 0;
	}

	/** 檢查字串不為 null 、空字串或只包含空格 */
	public final static boolean isTrimStrNotEmpty(String str)
	{
		boolean result = isStrNotEmpty(str);
		return result ? isStrNotEmpty(str.trim()) : result;
	}

	/** 檢查字串為 null 或空字串 */
	public final static boolean isStrEmpty(String str)
	{
		return !isStrNotEmpty(str);
	}

	/** 檢查字串為 null 、空字串或只包含空格 */
	public final static boolean isTrimStrEmpty(String str)
	{
		boolean result = isStrEmpty(str);
		return result ?  result : isStrEmpty(str.trim());
	}

	/** 把引數 str 轉換為安全字串:如果 str = null,則把它轉換為空字串 */
	public final static String safeString(String str)
	{
		if(str == null)
			str = "";
		
		return str;
	}

	/** 把引數 obj 轉換為安全字串:如果 obj = null,則把它轉換為空字串 */
	public final static String safeString(Object obj)
	{
		if(obj == null)
			return "";
		
		return obj.toString();
	}

	/** 把引數 str 轉換為安全字串並執行去除前後空格:如果 str = null,則把它轉換為空字串 */
	public final static String safeTrimString(String str)
	{
		return safeString(str).trim();
	}

	/** 檢查字串是否符合整數格式 */
	public final static boolean isStrNumeric(String str)
	{
		return str.matches("^0$|^\\-?[1-9]+[0-9]*$");
	}

	/** 檢查字串是否符合電子郵件格式 */
	public final static boolean isStrEmailAddress(String str)
	{
		return str.matches("^[a-z0-9_\\-]+(\\.[_a-z0-9\\-]+)*@([_a-z0-9\\-]+\\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$");
	}

	/** 檢查字串是否符合 IP 地址格式 */
	public final static boolean isStrIPAddress(String str)
	{
		return str.matches("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
	}

	/** 檢查字串是否符合 HTML 超連結元素格式 */
	public final static boolean isStrLink(String str)
	{
		return str.matches("<a[^>]*href=\\\"[^\\s\\\"]+\\\"[^>]*>[^<]*<\\/a>");
	}

	/** 檢查字串是否符合 URL 格式 */
	public final static boolean isStrURL(String str)
	{
		return str.matches("^((https?|ftp|news):\\/\\/)?([a-z]([a-z0-9\\-]*\\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\\/[a-z0-9_\\-\\.~]+)*(\\/([a-z0-9_\\-\\.]*)(\\?[a-z0-9+_\\-\\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?$");
	}
	
	/** 遮蔽正規表示式的轉義字元(但不遮蔽 ignores 引數中包含的字元) */
	public static final String escapeRegexChars(String str, char ... ignores)
	{
		final char ESCAPE_CHAR	 = '\\';
		final char[] REGEX_CHARS = {'.', ',', '?', '+', '-', '*', '^', '$', '|', '&', '{', '}', '[', ']', '(', ')', '\\'};
		
		char[] regex_chars = REGEX_CHARS;
		
		if(ignores.length > 0)
		{
			Set<Character> cs = new HashSet<Character>(REGEX_CHARS.length);
			
			for(int i = 0; i < REGEX_CHARS.length; i++)
				cs.add(REGEX_CHARS[i]);
			for(int i = 0; i < ignores.length; i++)
				cs.remove(ignores[i]);
				
			int i		= 0;
			regex_chars = new char[cs.size()];
			Iterator<Character> it = cs.iterator();
			
			while(it.hasNext())
				regex_chars[i++] = it.next();				
		}
		
		StringBuilder sb = new StringBuilder();
		
		for(int i = 0; i < str.length(); i++)
		{
			char c = str.charAt(i);
			
			for(int j = 0; j < regex_chars.length; j++)
			{
				if(c == regex_chars[j])
				{
					sb.append(ESCAPE_CHAR);
					break;
				}
			}
			
			sb.append(c);
		}
		
		return sb.toString();
	}

	/** 符分割字串(分割符:" \t\n\r\f,;") */
	public final static String[] splitStr(String str)
	{
		return splitStr(str, " \t\n\r\f,;");
	}
	
	/** 符分割字串(分割符:由 delim 引數指定) */
	public final static String[] splitStr(String str, String delim)
	{
		StringTokenizer st	= new StringTokenizer(str, delim);
		String[] array		= new String[st.countTokens()];
		
		int i = 0;
		while(st.hasMoreTokens())
			array[i++] = st.nextToken();
		
		return array;
	}

	/** 呼叫 {@linkplain Thread#sleep(long)} 方法使當前執行緒睡眠 period 毫秒 <br>
	 * 
	 * 如果 {@linkplain Thread#sleep(long)} 方法被中斷則返回 false
	 * 
	 */
	public final static boolean waitFor(long period)
	{
		if(period > 0)
		{
			try
			{
				Thread.sleep(period);
			}
			catch(Exception e)
			{
				return false;
			}
		}
		else
			Thread.yield();
		
		return true;
	}

	/** 呼叫 {@linkplain Thread#sleep(long)} 方法使當前執行緒睡眠 period 個 unit 時間單元 <br>
	 * 
	 * 如果 {@linkplain Thread#sleep(long)} 方法被中斷則返回 false
	 * 
	 */
	public final static boolean waitFor(long period, TimeUnit unit)
	{
		return waitFor(unit.toMillis(period));
	}

	/** String -> Integer,如果轉換不成功則返回 null */
	public final static Integer str2Int(String s)
	{
		Integer returnVal;
		try {
			returnVal = Integer.decode(safeTrimString(s));
		} catch(Exception e) {
			returnVal = null;
		}
		return returnVal;
	}

	/** String -> int,如果轉換不成功則返回預設值 d */
	public final static int str2Int(String s, int d)
	{
		int returnVal;
		try {
			returnVal = Integer.parseInt(safeTrimString(s));
		} catch(Exception e) {
			returnVal = d;
		}
		return returnVal;
	}

	/** String -> int,如果轉換不成功則返回 0 */
	public final static int str2Int_0(String s)
	{
		return str2Int(s, 0);
	}

	/** String -> Short,如果轉換不成功則返回 null */
	public final static Short str2Short(String s)
	{
		Short returnVal;
		try {
			returnVal = Short.decode(safeTrimString(s));
		} catch(Exception e) {
			returnVal = null;
		}
		return returnVal;
	}

	/** String -> short,如果轉換不成功則返回預設值 d */
	public final static short str2Short(String s, short d)
	{
		short returnVal;
		try {
			returnVal = Short.parseShort(safeTrimString(s));
		} catch(Exception e) {
			returnVal = d;
		}
		return returnVal;
	}

	/** String -> short,如果轉換不成功則返回 0 */
	public final static short str2Short_0(String s)
	{
		return str2Short(s, (short)0);
	}

	/** String -> Long,如果轉換不成功則返回 null */
	public final static Long str2Long(String s)
	{
		Long returnVal;
		try {
			returnVal = Long.decode(safeTrimString(s));
		} catch(Exception e) {
			returnVal = null;
		}
		return returnVal;
	}

	/** String -> long,如果轉換不成功則返回預設值 d */
	public final static long str2Long(String s, long d)
	{
		long returnVal;
		try {
			returnVal = Long.parseLong(safeTrimString(s));
		} catch(Exception e) {
			returnVal = d;
		}
		return returnVal;
	}

	/** String -> long,如果轉換不成功則返回 0 */
	public final static long str2Long_0(String s)
	{
		return str2Long(s, 0L);
	}

	/** String -> Float,如果轉換不成功則返回 null */
	public final static Float str2Float(String s)
	{
		Float returnVal;
		try {
			returnVal = Float.valueOf(safeTrimString(s));
		} catch(Exception e) {
			returnVal = null;
		}
		return returnVal;
	}

	/** String -> float,如果轉換不成功則返回預設值 d */
	public final static float str2Float(String s, float d)
	{
		float returnVal;
		try {
			returnVal = Float.parseFloat(safeTrimString(s));
		} catch(Exception e) {
			returnVal = d;
		}
		return returnVal;
	}

	/** String -> float,如果轉換不成功則返回 0 */
	public final static float str2Float_0(String s)
	{
		return str2Float(s, 0F);
	}

	/** String -> Double,如果轉換不成功則返回 null */
	public final static Double str2Double(String s)
	{
		Double returnVal;
		try {
			returnVal = Double.valueOf(safeTrimString(s));
		} catch(Exception e) {
			returnVal = null;
		}
		return returnVal;
	}

	/** String -> double,如果轉換不成功則返回預設值 d */
	public final static double str2Double(String s, double d)
	{
		double returnVal;
		try {
			returnVal = Double.parseDouble(safeTrimString(s));
		} catch(Exception e) {
			returnVal = d;
		}
		return returnVal;
	}

	/** String -> double,如果轉換不成功則返回 0.0 */
	public final static double str2Double_0(String s)
	{
		return str2Double(s, 0D);
	}

	/** String -> Byte,如果轉換不成功則返回 null */
	public final static Byte str2Byte(String s)
	{
		Byte returnVal;
		try {
			returnVal = Byte.decode(safeTrimString(s));
		} catch(Exception e) {
			returnVal = null;
		}
		return returnVal;
	}

	/** String -> byte,如果轉換不成功則返回預設值 d */
	public final static byte str2Byte(String s, byte d)
	{
		byte returnVal;
		try {
			returnVal = Byte.parseByte(safeTrimString(s));
		} catch(Exception e) {
			returnVal = d;
		}
		return returnVal;
	}

	/** String -> byte,如果轉換不成功則返回 0 */
	public final static byte str2Byte_0(String s)
	{
		return str2Byte(s, (byte)0);
	}

	/** String -> Character,如果轉換不成功則返回 null */
	public final static Character str2Char(String s)
	{
		Character returnVal;
		try {
			returnVal = safeTrimString(s).charAt(0);
		} catch(Exception e) {
			returnVal = null;
		}
		return returnVal;
	}

	/** String -> char,如果轉換不成功則返回預設值 d */
	public final static char str2Char(String s, char d)
	{
		char returnVal;
		try {
			returnVal = safeTrimString(s).charAt(0);
		} catch(Exception e) {
			returnVal = d;
		}
		return returnVal;
	}

	/** String -> char,如果轉換不成功則返回 0 */
	public final static char str2Char_0(String s)
	{
		return str2Char(s, Character.MIN_VALUE);
	}

	/** String -> Boolean,如果轉換不成功則返回 null */
	public final static Boolean str2Boolean(String s)
	{
		return Boolean.valueOf(safeTrimString(s));
	}

	/** String -> boolean,如果轉換不成功則返回預設值 d */
	public final static boolean str2Boolean(String s, boolean d)
	{
		s = safeTrimString(s);
		
		if(s.equalsIgnoreCase("true"))
			return true;
		else if(s.equalsIgnoreCase("false"))
			return false;
		
		return d;
	}

	/** String -> boolean,如果轉換不成功則返回 0 */
	public final static boolean str2Boolean_False(String s)
	{
		return str2Boolean(s, false);
	}

	/** String -> java.util.Date, str 的格式由 format  定義 */
	public final static Date str2Date(String str, String format)
	{
		Date date = null;
		
		try
		{
			DateFormat df = new SimpleDateFormat(format);
			date = df.parse(safeTrimString(str));
		}
		catch(Exception e)
		{

		}

		return date;
	}

	/** String -> java.util.Date,由函式自身判斷 str 的格式 */
	public final static Date str2Date(String str)
	{
		Date date = null;

		try
		{
			final char SEPARATOR	= '-';
			final String[] PATTERN	= {"yyyy", "MM", "dd", "HH", "mm", "ss", "SSS"};
			String[] values			= safeTrimString(str).split("\\D");
			String[] element		= new String[values.length];
			
			int length = 0;
			for(String e : values)
			{
				e = e.trim();
				if(e.length() != 0)
				{
					element[length++] = e;
					if(length == PATTERN.length)
						break;
				}
			}

			if(length > 0)
			{
	       		StringBuilder value	= new StringBuilder();

				if(length > 1)
				{
        			for(int i = 0; i < length; ++i)
        			{
        				value.append(element[i]);
        				value.append(SEPARATOR);
        			}
				}
				else
				{
					String src	= element[0];
					int remain	= src.length();
					int pos		= 0;
					int i		= 0;

					for(i = 0; remain > 0 && i < PATTERN.length; ++i)
					{
						int p_length	= PATTERN[i].length();
						int v_length	= Math.min(p_length, remain);
						String v 		= src.substring(pos, pos + v_length);
						pos			+= v_length;
						remain 		-= v_length;

						value.append(v);
						value.append(SEPARATOR);
					}

					length = i;
				}

     			StringBuilder format = new StringBuilder();

     			for(int i = 0; i < length; ++i)
				{
					format.append(PATTERN[i]);
					format.append(SEPARATOR);
				}

				date = str2Date(value.toString(), format.toString());
			}
		}
		catch(Exception e)
		{

		}

		return date;
	}
	
	/** String -> java.util.Date,由 Patterns 指定可能的日期格式 */
	public final static Date str2Date(String str, String[] Patterns)
	{
		Date date = null;
		
		for(int i = 0; i < Patterns.length; ++i)
		{
			date = str2Date(str, Patterns[i]);

			if( date != null)
				break;
		}

		return date;
	}
	
	/** String -> java.util.Date,由 GeneralHelper.SHORT_DATE_PATTERN 指定可能的日期格式 */
	public final static Date str2ShortDate(String str)
	{
		return str2Date(str, SHORT_DATE_PATTERN);
	}

	/** String -> java.util.Date,由 GeneralHelper.LONG_DATE_PATTERN 指定可能的日期格式 */
	public final static Date str2LongDate(String str)
	{
		return str2Date(str, LONG_DATE_PATTERN);
	}

	/** String -> java.util.Date,由 GeneralHelper.LONG_DATE_PATTERN_WITH_MILSEC 指定可能的日期格式 */
	public final static Date str2LongDateWithMilliSecond(String str)
	{
		return str2Date(str, LONG_DATE_PATTERN_WITH_MILSEC);
	}
	
	/** 型別轉換處理器介面 */
	public static interface TypeHandler<T>
	{
		T handle(String v);
	}
	
	/** String -> Any,字串轉換為 8 種基礎資料型別、及其包裝類 {@link Date}、 或 {@link String} 
	 * 
	 * @param type	: 目標型別的 {@link Class} 物件
	 * @param v		: 要轉換的字串
	 * @return		: 轉換結果,如果轉換不成功返回 null
	 * @throws 		: 如果目標型別不支援丟擲 {@link IllegalArgumentException}
	 * 
	 */
	public static final <T> T str2Object(Class<T> type, String v)
	{
		return str2Object(type, v, null);
	}
	
	/** String -> Any,如果 handler 為 null 則把字串轉換為 8 種基礎資料型別、及其包裝類、 {@link Date} 或 {@link String},
	 * 			      如果 handler 不為 null 則由 handler 執行轉換 
	 * 
	 * @param type	: 目標型別的 {@link Class} 物件
	 * @param v		: 要轉換的字串
	 * @param handler	: 型別轉換處理器
	 * @return		: 轉換結果,如果轉換不成功返回 null
	 * @throws 		: 如果目標型別不支援丟擲 {@link IllegalArgumentException}
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static final <T> T str2Object(Class<T> type, String v, TypeHandler<T> handler)
	{
		Object param = null;
		
		if(handler != null)
			return handler.handle(v);
		
		if(type == String.class)
			param =  safeTrimString(v);
		else if(type == int.class)
			param =  str2Int_0(v);
		else if(type == long.class)
			param =  str2Long_0(v);
		else if(type == byte.class)
			param =  str2Byte_0(v);
		else if(type == char.class)
			param =  str2Char_0(v);
		else if(type == float.class)
			param =  str2Float_0(v);
		else if(type == double.class)
			param =  str2Double_0(v);
		else if(type == short.class)
			param =  str2Short_0(v);
		else if(type == boolean.class)
			param =  str2Boolean_False(v);
		else if(type == Integer.class)
			param =  str2Int(v);
		else if(type == Long.class)
			param =  str2Long(v);
		else if(type == Byte.class)
			param =  str2Byte(v);
		else if(type == Character.class)
			param =  str2Char(v);
		else if(type == Float.class)
			param =  str2Float(v);
		else if(type == Double.class)
			param =  str2Double(v);
		else if(type == Short.class)
			param =  str2Short(v);
		else if(type == Boolean.class)
			param =  str2Boolean(v);
		else if(Date.class.isAssignableFrom(type))
			param =  str2Date(v);
		else
			throw new IllegalArgumentException(String.format("object type '%s' not valid", type));
		
		return (T)param;
	}

	/** Any -> Object[] <br>
	 * 
	 *  obj == null					: 返回 Object[] {null} <br>
	 *  obj 為物件陣列				: 強制轉換為 Object[], 並返回自身 <br>
	 *  obj 為基礎型別陣列			: 返回 Object[], 其元素型別為基礎型別的包裝類 <br>
	 *  obj 為 {@link Collection}	: 通過 toArray() 方法返回 Object[] <br>
	 *  obj 為 {@link Iterable}		: 遍歷 {@link Iterable}, 並返回包含其所有元素的 Object[] <br>
	 *  obj 為 {@link Iterator}		: 遍歷 {@link Iterator}, 並返回包含其所有元素的 Object[] <br>
	 *  obj 為 {@link Enumeration}	: 遍歷 {@link Enumeration}, 並返回包含其所有元素的 Object[] <br>
	 *  obj 為普通物件				: 返回 Object[] {obj} <br>
	 * 
	 * @param obj	: 任何物件
	 * 
	 */
	public static final Object[] object2Array(Object obj)
	{
		Object[] array;
		
		if(obj == null)
			array = new Object[] {obj};
		else if(obj.getClass().isArray())
		{
			Class<?> clazz = obj.getClass().getComponentType();
			
			if(Object.class.isAssignableFrom(clazz))
				array = (Object[])obj;
			else
			{
				int length = Array.getLength(obj);
				
				if(length > 0)
				{
					array = new Object[length];
					
					for(int i = 0; i < length; i++)
						array[i] = Array.get(obj, i);
				}
				else
					array = new Object[0];
			}
		}
		else if(obj instanceof Collection<?>)
			array = ((Collection<?>)obj).toArray();
		else if(obj instanceof Iterable<?>)
		{
			List<Object> list = new ArrayList<Object>();
			Iterator<?> it = ((Iterable<?>)obj).iterator();
			
			while(it.hasNext())
				list.add(it.next());
			
			array = list.toArray();
		}
		else if(obj instanceof Iterator)
		{
			List<Object> list = new ArrayList<Object>();
			Iterator<?> it = (Iterator<?>)obj;
			
			while(it.hasNext())
				list.add(it.next());
			
			array = list.toArray();
		}
		else if(obj instanceof Enumeration<?>)
		{
			List<Object> list = new ArrayList<Object>();
			Enumeration<?> it = (Enumeration<?>)obj;
			
			while(it.hasMoreElements())
				list.add(it.nextElement());
			
			array = list.toArray();
		}
		else
			array = new Object[] {obj};
		
		return array;
	}

	/** 返回 date 加上 value 天后的日期(清除時間資訊) */
	public final static Date addDate(Date date, int value)
	{
		return addDate(date, value, true);
	}

	/** 返回 date 加上 value 天后的日期,trimTime 指定是否清除時間資訊 */
	public final static Date addDate(Date date, int value, boolean trimTime)
	{
		return addTime(date, Calendar.DATE, value, trimTime);

	}

	/** 返回 date 加上 value 個 field 時間單元后的日期(不清除時間資訊) */
	public final static Date addTime(Date date, int field, int value)
	{
		return addTime(date, field, value, false);
	}

	/** 返回 date 加上 value 個 field 時間單元后的日期,trimTime 指定是否去除時間資訊 */
	public final static Date addTime(Date date, int field, int value, boolean trimTime)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(field, value);

		if(trimTime)
		{
        		c.set(Calendar.HOUR, 0);
        		c.set(Calendar.MINUTE, 0);
        		c.set(Calendar.SECOND, 0);
        		c.set(Calendar.MILLISECOND, 0);
		}

		return c.getTime();

	}

	/** java.util.Date -> String,str 的格式由 format  定義 */
	public final static String date2Str(Date date, String format)
	{
		DateFormat df	= new SimpleDateFormat(format);
		return df.format(date);
	}

	/** 修整 SQL 語句字串:' -> '',(includeWidlcard 指定是否對星號和問號作轉換:* -> %, ? -> _) */
	public static final String regularSQLStr(String str, boolean includeWidlcard)
	{
		str = str.replace("'", "''");

		if(includeWidlcard)
		{
			str = str.replace('*', '%');
			str = str.replace('?', '_');
		}

		return str;
	}
	
	/** 獲取 clazz 的 {@link ClassLoader} 物件,如果為 null 則返回當前執行緒的 Context {@link ClassLoader} */
	public static final ClassLoader getClassLoader(Class<?> clazz)
	{
		ClassLoader loader = clazz.getClassLoader();
		
		if(loader == null)
			loader = Thread.currentThread().getContextClassLoader();
		
		return loader;
	}
	
	/** 載入類名為  className 的 {@link Class} 物件,如果載入失敗則返回 null */
	public static final Class<?> loadClass(String className)
	{
		Class<?> clazz		= null;
		ClassLoader loader	= getClassLoader(GeneralHelper.class);
		
		try
		{
			clazz = loader.loadClass(className);
		}
		catch(ClassNotFoundException e)
		{
			
		}
		
		return clazz;
	}

	/** 用 {@linkplain Class#forName(String)} 載入 {@link Class} 物件,如果載入失敗則返回 null */
	public static final Class<?> classForName(String name)
	{
		Class<?> clazz = null;
		
		try
		{
			clazz = Class.forName(name);
		}
		catch(ClassNotFoundException e)
		{
			
		}
		
		return clazz;
	}

	/** 用 {@linkplain Class#forName(String, boolean, ClassLoader)} 載入 {@link Class} 物件,如果載入失敗則返回 null */
	public static final Class<?> classForName(String name, boolean initialize, ClassLoader loader)
	{
		Class<?> clazz = null;
		
		try
		{
			clazz = Class.forName(name, initialize, loader);
		}
		catch(ClassNotFoundException e)
		{
			
		}
		
		return clazz;
	}

	/** 獲取 clazz 資源環境中 resPath 相對路徑的 URL 物件 */
	public static final URL getClassResource(Class<?> clazz, String resPath)
	{
		URL url = clazz.getResource(resPath);
		
		if(url == null)
		{
			ClassLoader loader = clazz.getClassLoader();
			if(loader != null) url = loader.getResource(resPath);
			
			if(url == null)
			{
				loader = Thread.currentThread().getContextClassLoader();
				if(loader != null) url = loader.getResource(resPath);
			}
		}

		return url;
	}

	/** 獲取 clazz 資源環境中 resPath 相對路徑的 URL 物件列表 */
	public static final List<URL> getClassResources(Class<?> clazz, String resPath)
	{
		List<URL> urlList		= new ArrayList<URL>();
		Enumeration<URL> urls	= null;
		
		try
		{
    		ClassLoader loader = clazz.getClassLoader();
    		if(loader != null) urls = loader.getResources(resPath);
    		
    		if(urls == null || !urls.hasMoreElements())
    		{
    			loader = Thread.currentThread().getContextClassLoader();
    			if(loader != null) urls = loader.getResources(resPath);
    		}
		}
		catch(IOException e)
		{
			throw new RuntimeException(e);
		}
		
		if(urls != null)
		{
			while(urls.hasMoreElements())
				urlList.add(urls.nextElement());
		}

		return urlList;
	}

	/** 獲取 clazz 資源環境中 resPath 的 {@link InputStream} */
	public static final InputStream getClassResourceAsStream(Class<?> clazz, String resPath)
	{
		InputStream is = clazz.getResourceAsStream(resPath);
		
		if(is == null)
		{
			ClassLoader loader = clazz.getClassLoader();
			if(loader != null) is = loader.getResourceAsStream(resPath);
			
			if(is == null)
			{
				loader = Thread.currentThread().getContextClassLoader();
				if(loader != null) is = loader.getResourceAsStream(resPath);
			}
		}

		return is;
	}

	/** 獲取 clazz 資源環境中 resPath 相對路徑的 URL 絕對路徑(返還的絕對路徑用 UTF-8 編碼) */
	public static final String getClassResourcePath(Class<?> clazz, String resPath)
	{
		return getClassResourcePath(clazz, resPath, DEFAULT_ENCODING);
	}

	/** 獲取 clazz 資源環境中 resPath 相對路徑的 URL 絕對路徑(返還的絕對路徑用 pathEnc 編碼) */
	public static final String getClassResourcePath(Class<?> clazz, String resPath, String pathEnc)
	{
		String path = null;

		try
		{
			URL url = getClassResource(clazz, resPath);
			
			if(url != null)
			{
				path = url.getPath();
				path = URLDecoder.decode(path, pathEnc);
			}
		}
		catch(UnsupportedEncodingException e)
		{
			throw new RuntimeException(e);
		}

		return path;
	}

	/** 獲取 clazz 資源環境中 resPath 相對路徑的 URL 絕對路徑列表(返還的絕對路徑用 UTF-8 編碼) */
	public static final List<String> getClassResourcePaths(Class<?> clazz, String resPath)
	{
		return getClassResourcePaths(clazz, resPath, DEFAULT_ENCODING);
	}

	/** 獲取 clazz 資源環境中 resPath 相對路徑的 URL 絕對路徑列表(返還的絕對路徑用 pathEnc 編碼) */
	public static final List<String> getClassResourcePaths(Class<?> clazz, String resPath, String pathEnc)
	{
		List<String> pathList = new ArrayList<String>();

		try
		{
			List<URL> urlList = getClassResources(clazz, resPath);
			
			for(URL url : urlList)
			{
				String path = URLDecoder.decode(url.getPath(), pathEnc);
				pathList.add(path);
			}
		}
		catch(UnsupportedEncodingException e)
		{
			throw new RuntimeException(e);
		}

		return pathList;
	}

	/** 獲取 clazz 資源環境的當前 URL 絕對路徑(返回的絕對路徑用 pathEnc 編碼) */
	public static final String getClassPath(Class<?> clazz)
	{
		return getClassResourcePath(clazz, ".");
	}

	/** 獲取 resource 資源的 locale 本地化檔案中名字為 key 的字串資源,並代入 params 引數 */
	public static final String getResourceMessage(Locale locale, String resource, String key, Object ... params)
	{
		ResourceBundle bundle = ResourceBundle.getBundle(resource, locale);
		String msg = bundle.getString(key);

		if(params != null && params.length > 0)
			msg = MessageFormat.format(msg, params);

		return msg;
	}

	/** 獲取 resource 資源的預設本地化檔案中名字為 key 的字串資源,並代入 params 引數 */
	public static final String getResourceMessage(String resource, String key, Object ... params)
	{
		return getResourceMessage(Locale.getDefault(), resource, key, params);
	}

	/** 獲取 e 異常的堆疊列表,最帶的堆疊層數由 levels 指定 */
	public static final List<String> getExceptionMessageStack(Throwable e, int levels)
	{
		List<String> list = new ArrayList<String>();

		if(levels == 0)
			levels = Integer.MAX_VALUE;

		for(int i = 0; i < levels; ++i)
		{
			StringBuilder sb = new StringBuilder();

			if(i > 0)
				sb.append("Caused by -> ");

			sb.append(e.getClass().getName());
			String msg = e.getLocalizedMessage();
			if(msg != null)
				sb.append(": ").append(msg);

			list.add(sb.toString());

			e = e.getCause();
			if(e == null)
				break;
		}

		return list;
	}

	/** 獲取 e 異常的整個堆疊列表 */
	public static final List<String> getExceptionMessageStack(Throwable e)
	{
		return getExceptionMessageStack(e, 0);
	}

	/** 輸出 e 異常的 levels 層堆疊列表到 ps 中 */
	public static final void printExceptionMessageStack(Throwable e, int levels, PrintStream ps)
	{
		List<String> list = getExceptionMessageStack(e, levels);

		for(String msg : list)
			ps.println(msg);
	}

	/** 輸出 e 異常的 levels 層堆疊列表到標準錯誤流中 */
	public static final void printExceptionMessageStack(Throwable e, int levels)
	{
		printExceptionMessageStack(e, levels, System.err);
	}

	/** 輸出 e 異常的整個堆疊列表到 ps 中 */
	public static final void printExceptionMessageStack(Throwable e, PrintStream ps)
	{
		printExceptionMessageStack(e, 0, ps);
	}

	/** 輸出 e 異常的整個堆疊列表到標準錯誤流中 */
	public static final void printExceptionMessageStack(Throwable e)
	{
		printExceptionMessageStack(e, 0);
	}
	
	/** 把元素新增到 {@link Map} 中,不保證執行緒安全(不替換原值) */
	public static final <K, V> boolean tryPut(Map<K, V> map, K key, V value)
	{
		return tryPut(map, key, value, false);
	}
	
	/** 把元素新增到 {@link Map} 中,不保證執行緒安全 */
	public static final <K, V> boolean tryPut(Map<K, V> map, K key, V value, boolean replace)
	{
		if(replace || !map.containsKey(key))
		{
			map.put(key, value);
			return true;
		}
		
		return false;
	}

	/** 把元素新增到 {@link Map} 中,並保證執行緒安全(不替換原值) */
	public static final <K, V> boolean syncTryPut(Map<K, V> map, K key, V value)
	{
		return syncTryPut(map, key, value, false);
	}
	
	/** 把元素新增到 {@link Map} 中,並保證執行緒安全 */
	public static final <K, V> boolean syncTryPut(Map<K, V> map, K key, V value, boolean replace)
	{
		synchronized(map)
		{
			return tryPut(map, key, value, replace);
		}
	}

	/** 把元素新增到 {@link Map} 中,不保證執行緒安全(不替換原值) */
	public static final <K, V> int tryPutAll(Map<K, V> map, Map<K, V> src)
	{
		return tryPutAll(map, src, false);
	}
	
	/** 把元素新增到 {@link Map} 中,不保證執行緒安全 */
	public static final <K, V> int tryPutAll(Map<K, V> map, Map<K, V> src, boolean replace)
	{
		if(replace)
		{
			map.putAll(src);
			return src.size();
		}
		
		int count = 0;
		Set<Entry<K, V>> entries = src.entrySet();
		
		for(Entry<K, V> e : entries)
		{
			if(!map.containsKey(e.getKey()))
			{
				map.put(e.getKey(), e.getValue());
				++count;
			}
		}
		
		return count;
	}

	/** 把元素新增到 {@link Map} 中,並保證執行緒安全(不替換原值) */
	public static final <K, V> int syncTryPutAll(Map<K, V> map, Map<K, V> src)
	{
		return syncTryPutAll(map, src, false);
	}
	
	/** 把元素新增到 {@link Map} 中,並保證執行緒安全 */
	public static final <K, V> int syncTryPutAll(Map<K, V> map, Map<K, V> src, boolean replace)
	{
		synchronized(map)
		{
			return tryPutAll(map, src, replace);
		}
	}

	/** 從 {@link Map} 中刪除元素,不保證執行緒安全 */
	public static final <K, V> boolean tryRemove(Map<K, V> map, K key)
	{
		if(map.containsKey(key))
		{
			map.remove(key);
			return true;
		}
		
		return false;
	}

	/** 從 {@link Map} 中刪除元素,並保證執行緒安全 */
	public static final <K, V> boolean syncTryRemove(Map<K, V> map, K key)
	{
		synchronized(map)
		{
			return tryRemove(map, key);
		}
	}

	/** 清空 {@link Map},不保證執行緒安全 */
	public static final <K, V> void tryClear(Map<K, V> map)
	{
		map.clear();
	}

	/** 清空 {@link Map},並保證執行緒安全 */
	public static final <K, V> void syncTryClear(Map<K, V> map)
	{
		synchronized(map)
		{
			tryClear(map);
		}
	}

	/** 獲取當前 JVM 程式的 ID */
	public static final int getProcessId()
	{
		return Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
	}

	/** 獲取當前 JVM 程式的 Java 版本 */
	public static final String getJavaVersion()
	{
		return System.getProperty("java.version");
	}

	/** 獲取當前作業系統的名稱 */
	public static final String getOSName()
	{
		return System.getProperty("os.name");
	}

	/** 檢查當前作業系統是否為 Windows 系列 */
	public static final boolean isWindowsPlatform()
	{
		// return CURRENT_OS.toUpperCase().indexOf("WINDOWS") != -1;
		
		return File.pathSeparatorChar == ';';
	}

	/** 按拼音排序的字串比較器 */
	public static class PinYinComparator implements Comparator<String>
	{
		@Override
		public int compare(String o1, String o2)
		{
			java.text.Collator cmp = java.text.Collator.getInstance(Locale.CHINA);
			return cmp.compare(o1, o2);
		}
	}

	/** 按檔名稱進行檔案篩選的檔案過濾器,建構函式引數 name 指定檔名的正規表示式 */
	public static class FileNameFileFilter implements FileFilter
	{
		protected static final int FLAGS = IS_WINDOWS_PLATFORM ? Pattern.CASE_INSENSITIVE : 0;

		Pattern pattern;

		public FileNameFileFilter(String name)
		{
			String exp = name;
			exp = exp.replace('.', '#');
			exp = exp.replaceAll("#", "\\\\.");
			exp = exp.replace('*', '#');
			exp = exp.replaceAll("#", ".*");
			exp = exp.replace('?', '#');
			exp = exp.replaceAll("#", ".?");
			exp = "^" + exp + "$";

			pattern = Pattern.compile(exp, FLAGS);
		}

		@Override
		public boolean accept(File file)
		{
			Matcher matcher = pattern.matcher(file.getName());
			return matcher.matches();
		}
	}
	
	/**
	 * 
	 * @Description:比較兩個時間點 如果secondDate表示的時間等於此 firstDate 表示的時間,則返回 0 值; 如果此
	 *                      firstDate 的時間在引數<secondDate>表示的時間之前,則返回小於 0 的值; 如果此
	 *                      firstDate 的時間在引數<secondDate>表示的時間之後,則返回大於 0 的值
	 * @param firstDate
	 * @param secondDate
	 * @ReturnType int
	 * @author:
	 * @Created 2012 2012-9-20上午08:34:33
	 */
	public static int compare(Date firstDate, Date secondDate) {

		Calendar firstCalendar = null;
		/** 使用給定的 Date 設定此 Calendar 的時間。 **/
		if (firstDate != null) {
			firstCalendar = Calendar.getInstance();
			firstCalendar.setTime(firstDate);
		}

		Calendar secondCalendar = null;
		/** 使用給定的 Date 設定此 Calendar 的時間。 **/
		if (firstDate != null) {
			secondCalendar = Calendar.getInstance();
			secondCalendar.setTime(secondDate);
		}

		try {
			/**
			 * 比較兩個 Calendar 物件表示的時間值(從曆元至現在的毫秒偏移量)。 如果參數列示的時間等於此 Calendar
			 * 表示的時間,則返回 0 值; 如果此 Calendar 的時間在參數列示的時間之前,則返回小於 0 的值; 如果此
			 * Calendar 的時間在參數列示的時間之後,則返回大於 0 的值
			 * **/
			return firstCalendar.compareTo(secondCalendar);
		} catch (NullPointerException e) {
			throw new IllegalArgumentException(e);
		} catch (IllegalArgumentException e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * List<Object>去重
	 * 	Object 必須實現equals和hashCode方法
	 * @param list
	 */
	public static void removeDuplicateWithObject(List list) {
		   Set set = new HashSet();
		   List newList = new ArrayList();
		   for (Iterator iter = list.iterator(); iter.hasNext();) {
		          Object element = iter.next();
		          if (set.add(element))
		             newList.add(element);
		       }
		      list.clear();
		      list.addAll(newList);
		}
	
	/**
	 * 將map轉換成List
	 * @param map
	 * @return
	 */
	public static List<Object> mapByList(HashMap<String, Object> map){
		List<Object> list = new ArrayList<Object>();
		Set<Entry<String, Object>> set = map.entrySet();
		Iterator<Entry<String, Object>> it = set.iterator();
		//將map物件裡面的屬性迴圈遍歷出來
		while(it.hasNext()){
			Entry<String, Object> entry = it.next();
			//得到value值,裝到list裡面,也可以entry.getKey()。
			//如果2個都需要裝。可以弄成一個物件來裝
			list.add(entry.getValue());
		}
		return list;
	}
	
	/**
	 * 將Date轉換成String【yyyy-MM-dd】
	 * @param date
	 * @return
	 */
	public static String dateConvertString(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String str=sdf.format(date);
		return str;
	}
	
	/**
	 * 根據指定長度 分隔字串
	 * 
	 * @param str
	 *            需要處理的字串
	 * @param length
	 *            分隔長度
	 * 
	 * @return 字串集合
	 */
	public static List<String> splitString(String str, int length) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < str.length(); i += length) {
			int endIndex = i + length;
			if (endIndex <= str.length()) {
				list.add(str.substring(i, i + length));
			} else {
				list.add(str.substring(i, str.length() - 1));
			}
		}
		return list;
	}

	/**
	 * 將字串List轉化為字串,以分隔符間隔.
	 * 
	 * @param list
	 *            需要處理的List.
	 *            
	 * @param separator
	 *            分隔符.
	 * 
	 * @return 轉化後的字串
	 */
	public static String toString(List<String> list, String separator) {
		StringBuffer stringBuffer = new StringBuffer();
		for (String str : list) {
			stringBuffer.append(separator + str);
		}
		stringBuffer.deleteCharAt(0);
		return stringBuffer.toString();
	}
	
	/** 
	   * Description: 隨機從陣列中取出指定的不重複的n個數。 
	   * @param ArrayList 原始陣列 
	   * @param int n 隨機抽取的個數 
	   * @return 返回抽取的陣列 
	   */  
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List getRandomArray(List list)  
	{  
		Collections.shuffle(list);	//洗牌
		return list;
	}
	
	/**
	 * 擷取指定的長度
	 * @param list
	 * @param num
	 * @return
	 */
	public static List subList(List list ,int num)  {
		List newList = getRandomArray(list);
		return newList.subList(0, num);
	}

}

相關文章