/**
* 一些通用的函数
*/
public class FunctionUtil {
private static long lastClickTime = 0;
/**
* 开始用的这种,后来就不限制,调用checkPhone方法。 判断手机格式是否正确,在注册和修改手机号码的时候用到
*
* @param mobiles
* @return true:正确的手机号码
*/
public static boolean isMobileNO(String mobiles) {
// Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Pattern p = Pattern.compile("^(1(([358][0-9])|(4[57])))\\d{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}
/**
* 校验手机号码
*
* @param context
* @param phone
* @return
*/
public static boolean checkPhone(Context context, String phone) {
if (null == phone || "".equals(phone) || "".equals(phone.trim())) {
FunctionUtil.toastMessage(context, "请输入手机号码");
return false;
} else if (phone.length() != 11 || !phone.startsWith("1")) {
FunctionUtil.toastMessage(context, "手机号码格式不对");
return false;
}
return true;
}
/**
* 是否包含非法字符 注册的时候判断用户名是否包含特殊字符
*
* @return true :包含特殊字符,fasle就是不包含
*/
public static boolean containInvalidChars(String str) {
if (str == null || "".equals(str))
return false;
String SPECIAL_STR = "#~!@%^&*();'\"?><[]{}\\|,:/=+—“”‘.`$;,。!@#¥%……&*()——+?";
for (int i = 0; i < str.length(); i++) {
if (SPECIAL_STR.indexOf(str.charAt(i)) != -1) {
return true;
}
}
return false;
}
/**
* 判断是否全是数字
*
* @param str
* @return true :全是数字
*/
public static boolean isNumer(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
/**
* 判断email格式是否正确
*
* @param email
* @return
*/
public static boolean isEmail(String email) {
final String str_email = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
Pattern p = Pattern.compile(str_email);
Matcher m = p.matcher(email);
return m.matches();
}
// Toast提示语句
public static void toastMessage(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* 打印信息到控制台 测试的时候用到,打包的时候注释里面的那句代码
*/
public static void sysMessage(String message) {
System.out.println(message);
}
public static String getTime() {
Calendar c = Calendar.getInstance(); // 获取东八区时间
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return s.format(c.getTime()); // 当前日期
}
public static String getTime2() {
Calendar c = Calendar.getInstance(); // 获取东八区时间
SimpleDateFormat s = new SimpleDateFormat("MM-dd HH:mm");
return s.format(c.getTime()); // 当前日期
}
/**
* 格式化时间,去掉秒
*
* @param time
* @return
*/
public static String deleteSecond(String time) {
if (time == null || "null".equals(time) || "".equals(time))
return "";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dDate = null;
try {
dDate = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return format.format(dDate);
}
/**
* 格式化时间,去掉年,秒
*
* @param time
* @return
*/
public static String deleteNM(String time) {
if (time == null || "null".equals(time) || "".equals(time))
return "";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dDate = null;
try {
dDate = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
format = new SimpleDateFormat("MM-dd HH:mm");
return format.format(dDate);
}
/**
* 格式化时间,去小时,分钟,秒
*
* @param time
* @return
*/
public static String deleteHHMMSS(String time) {
if (time == null || "null".equals(time) || "".equals(time))
return "";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dDate = null;
try {
dDate = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(dDate);
}
/**
* 格式化时间,得到几月几日
*
* @param time
* @return
*/
public static String getMonthAndDate(String time) {
if (!strNotNull(time)) {
return "";
}
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dDate = null;
try {
dDate = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
format = new SimpleDateFormat("MM-dd");
return format.format(dDate);
}
/**
* 格式化时间,获取小时和分钟
*
* @param time
* @return
*/
public static String getHourAndMinute(String time) {
if (!strNotNull(time)) {
return "";
}
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dDate = null;
try {
dDate = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
format = new SimpleDateFormat("HH:mm");
return format.format(dDate);
}
/**
* 获取要显示的时间
*
* @param days
* 天
* @param diff
* 到的的时间 (微秒)
* @param hours
* 时
* @param minutes
* 分
* @param seconds
* 秒
* @return
*/
public static String getShowTime(long days, long diff, long hours, long minutes, long seconds) {
days = diff / (1000 * 60 * 60 * 24);
hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
if (days > 0) {
return days + "天" + hours + "时" + minutes + "分" + seconds + "秒";
} else if (hours <= 0 && minutes > 0) {
return minutes + "分" + seconds + "秒";
} else if (hours <= 0 && minutes <= 0) {
return seconds + "秒";
} else
return hours + "时" + minutes + "分" + seconds + "秒";
}
// 截取字符串
public static String captureString(String str) {
if (strNotNull(str) && str.length() > 4) {
return str.substring(str.length() - 3);
} else {
return "";
}
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
// 生成一注双色球(6+1)
public static int[] randomSSQ() {
List<Integer> list = new ArrayList<Integer>();
int[] array = new int[7];
int number = -1;
Random random = new Random();
for (int i = 0; i < 6; i++) {
number = (random.nextInt(33) + 1); // [1,34)
// System.out.println("for--------------------");
while (true) {
// System.out.println("while--------------------" + number);
if (list.contains(number)) {
number = (random.nextInt(33) + 1); // [1,34)
} else {
list.add(number);
array[i] = number;
break;
}
}
}
list = null;
Arrays.sort(array, 0, 6);// 排序 [0,6)
array[6] = (random.nextInt(16) + 1); // [1,17)
return array;
}
// 生成一注大乐透数据(5+2)
public static int[] randomDLT() {
List<Integer> list = new ArrayList<Integer>();
int[] array = new int[7];
int number = -1;
Random random = new Random();
for (int i = 0; i < 5; i++) {
number = (random.nextInt(35) + 1); // [1,36)
// System.out.println("for--------------------");
while (true) {
// System.out.println("while--------------------" + number);
if (list.contains(number)) {
number = (random.nextInt(35) + 1); // [1,36)
} else {
list.add(number);
array[i] = number;
break;
}
}
}
list = null;
Arrays.sort(array, 0, 5);// 排序 [0,5)
array[5] = (random.nextInt(12) + 1); // [1,13)
number = (random.nextInt(12) + 1); // [1,13)
while (number == array[5]) {
number = (random.nextInt(12) + 1); // [1,13)
}
array[6] = number;
Arrays.sort(array, 5, 7);// 排序 [5,7)
return array;
}
// 生成一注快三数据(3个数字都在1-6范围内)
public static int[] randomK3(String playType) {
List<Integer> list = new ArrayList<Integer>();
int[] array = new int[3];
int number = -1;
Random random = new Random();
if("501".equals(playType)){//和值
for (int i = 0; i < 3; i++) {
number = (random.nextInt(6) + 1); // [1,7)
// System.out.println("for--------------------");
array[i] = number;
}
}else if("506".equals(playType)){//三不同号
for (int i = 0; i < 3; i++) {
number = (random.nextInt(6) + 1); // [1,7)
// System.out.println("for--------------------");
while (true) {
// System.out.println("while--------------------" + number);
if (list.contains(number)) {
number = (random.nextInt(6) + 1); // [1,7)
} else {
list.add(number);
array[i] = number;
break;
}
}
}
}else if("503".equals(playType)){//三同号单选
number = (random.nextInt(6) + 1); // [1,7)
for (int i = 0; i < 3; i++) {
// System.out.println("for--------------------");
array[i] = number;
}
}else if("507".equals(playType)){//二不同号
array = new int[2];
number = (random.nextInt(6) + 1); // [1,7)
array[0] = number;
list.add(number);
while (true) {
// System.out.println("while--------------------" + number);
if (list.contains(number)) {
number = (random.nextInt(6) + 1); // [1,7)
} else {
array[1] = number;
break;
}
}
}
Arrays.sort(array);// 排序
return array;
}
// 生成一注11选5数据(5个数字都在1-11范围内)
public static int[] random11choose5(String play_type) {
int numberLenth = 8;
if("208".equals(play_type)){
numberLenth = 8;
}else if("207".equals(play_type)){
numberLenth = 7;
}else if("206".equals(play_type)){
numberLenth = 6;
}else if("205".equals(play_type)){
numberLenth = 5;
}else if("204".equals(play_type)){
numberLenth = 4;
}else if("203".equals(play_type)){
numberLenth = 3;
}else if("202".equals(play_type)){
numberLenth = 2;
}
List<Integer> list = new ArrayList<Integer>();
int[] array = new int[numberLenth];
int number = -1;
Random random = new Random();
for (int i = 0; i < numberLenth; i++) {
number = (random.nextInt(11) + 1); // [1,12)
// System.out.println("for--------------------");
while (true) {
// System.out.println("while--------------------" + number);
if (list.contains(number)) {
number = (random.nextInt(11) + 1); // [1,12)
} else {
list.add(number);
array[i] = number;
break;
}
}
}
Arrays.sort(array);// 排序
return array;
}
/**
* 防止用户连续点击
*
* @return
*/
public static boolean isFastDoubleClick() {
if (lastClickTime == 0) {// 刚启动第一次运行
lastClickTime = System.currentTimeMillis();
return false;
}
// 以前在地方运行过,现在在另外一个Activity运行
long time = System.currentTimeMillis();
long timeDistanse = time - lastClickTime;
if (0 < timeDistanse && timeDistanse < 800) {
return true;
}
lastClickTime = time;
return false;
}
// 判断字符串是否为空,不为空返回true
public static Boolean strNotNull(String str) {
if (str == null || "null".equals(str) || "".equals(str.trim())) {
return false;
}
return true;
}
// 判断list是否为空,不为空返回true
public static Boolean listNotNull(List<Object> list) {
if (list == null || list.size() == 0) {
return false;
}
return true;
}
// 判断list是否为空,不为空返回true
public static Boolean listStringNotNull(List<String> list) {
if (list == null || list.size() == 0) {
return false;
}
return true;
}
// 判断array是否为空,不为空返回true
public static Boolean arrayNotNull(Object[] array) {
if (array == null || array.length == 0) {
return false;
}
return true;
}
// 判断map是否为空,不为空返回true
public static Boolean mapNotNull(Map map) {
if (map == null || map.size() == 0) {
return false;
}
return true;
}
/**
* list排序
*
* @param code
* @return
*/
public static ArrayList<String> sortArray(ArrayList<String> code) {
// 移除所有不是数字的
for (int i = 0; i < code.size(); i++)
code.set(i, code.get(i) == null ? "0" : code.get(i).replaceAll("//D+", ""));
for (int i = 0; i < code.size(); i++) {
for (int j = i; j < code.size(); j++) {
if (Integer.valueOf(code.get(i)) > Integer.valueOf(code.get(j))) {
String tmp = code.get(j);
code.set(j, code.get(i));
code.set(i, tmp);
}
}
}
return code;
}
}
公司Android项目公共通用的函数和方法
最新推荐文章于 2021-02-06 10:52:37 发布