Java开发过常用的基础函数--很有用 (一)

2014-11-24 08:51:43 · 作者: · 浏览: 0

package tools;

import java.util.*;
import java.text.*; //日期处理用到的包
import java.util.regex.*;
import java.lang.*;
import java.math.*;

/**
*

Title: 常用基础函数


*

Description: 以下全部是静态函数


*

Copyright: Copyright (c) 2005


*

Company: 静靖工作室


* “@deprecated Method方法名 is deprecated” 在方法的注释里加上此注释,表示此方法以后版本不再被支持。
* 文件注释只能写在import后,类定义前才能在javadoc中出现文件的注释
* @author 欧朝敬
* QQ:35712069
* 手机:13873195792
* @version 1.0
*/
public class BaseFunction {
public BaseFunction() {

}

/**
* 拼合一维数组为字符串。
* 拆分字串按指定字符分解到一维数组使用String类的split(String regex)
* @param param String[] 数组
* @param spilt String 字符串之单的分离符
* @return String
*/
public static String arrayToString(String[] param, String spilt) {
String rentunstring;
StringBuffer tempstr = new StringBuffer();
int len = param.length;
for (int i = 0; i < len - 1; i++) {
tempstr.append(param[i]);
tempstr.append(spilt);
}
tempstr.append(param[len - 1]);
rentunstring = tempstr.toString();
return rentunstring;
}

/**
* 产生在start和end之间的num个随机整数,返回值存在HashMap中。
* 示例
* HashMap hm=BaseFunction.random(1,100,5);
* Set set=hm.keySet();
* Iterator it=set.iterator();
* while(it.hasNext()){
* System.out.println(hm.get(it.next()));
* }
* @param start int 起始点
* @param end int 结束点
* @param num int 生成个数
* @return HashMap
*/
public static HashMap random(int start, int end, int num) {
HashMap hashMap = new HashMap();
for (int i = 0; i < num; i++) {
double sru = Math.random() * end;
int tag = Math.round((float) sru);
if (tag < start) {
i--;
} else {
hashMap.put(new Integer(i), new Integer(tag));
}
}
return hashMap;
}


/**
* 获取当前时间,返回时间格式(如果调用参数为true时返回yyyy-MM-dd HH:mm:ss
* ,否则为false时返回yyyy-MM-DD不带日期格式)
* @param time boolean
* @return String
* @throws Exception
*/
public static String getNowTime(boolean time) throws java.lang.Exception {
Date now = new Date();
String format = "";
//yyyy-MM-dd HH:mm:ss:S 年月日时分秒毫杪
if (time) {
format = "yyyy-MM-dd HH:mm:ss";
} else {
format = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String nowtime = sdf.format(now);
return nowtime;
}
/**
* 获取当前时间自己定义
*
* @param format 格式 例yyyy年MM月dd日 HH时mm分ss杪
* @return String
* @throws Exception
*/
public static String getNowTime(String format) {
String nowtime = "";
try {
Date now = new Date();
// String format = "";
// yyyy-MM-dd HH:mm:ss:S 年月日时分秒毫杪
// if (time) {
// format = "yyyy-MM-dd HH:mm:ss";
// } else {
// format = "yyyy-MM-dd";
// }
SimpleDateFormat sdf = new SimpleDateFormat(format);
nowtime = sdf.format(now);
} catch (Exception ex) {
ex.printStackTrace();
}
return nowtime;
}
/**
* 将HashMap内容转入数组,
* 示例
* HashMap hashMap = new HashMap();
* hashMap.put("ka", "bb");
* hashMap.put("kb", "cc");
* hashMap.put("jk", "fdsaf");
* hashMap.put("ak", "kkkkk");
* Object[] obj = BaseFunction.hashMapToArray(hashMap);
* for (int i = 0; i < obj.length; i++) {
* System.out.println(obj[i]);
* }
* @param param HashMap
* @return Object[]
*/
public static Object[] hashMapToArra