package com.wdkl.callingmainnursemanager.util;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import com.wdkl.callingmainnursemanager.BuildConfig;
import com.wdkl.callingmainnursemanager.MyApplication;
import com.wdkl.callingmainnursemanager.R;
import com.wdkl.callingmainnursemanager.common.Constants;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 类描述:字符串操作工具包
* 创建人:Waderson Shll (TEL:15675117662)
* 创建时间:2017-11-2
* 特别提醒:如有需要该类可任意调用;在未通知本人的情况下该类禁止任何修改!
*
* @version 1.0
*/
public class StringUtils {
private final static Pattern emailer = Pattern
.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
private final static Pattern backCard = Pattern
.compile(" /^(\\[0-9]{16}|\\[0-9]{18}|\\[0-9]{19})$/;");
// private final static SimpleDateFormat dateFormater = new
// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// private final static SimpleDateFormat dateFormater2 = new
// SimpleDateFormat("yyyy-MM-dd");
private final static ThreadLocal dateFormater = new ThreadLocal() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
private final static ThreadLocal dateFormater2 = new ThreadLocal() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
public static String getAppVersionName(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
return info.versionName;
// return info.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return 1.0 + "";
}
public static String getAllVersionName() {
String str = "\r\nApp名称: " + MyApplication.getAppContext().getString(R.string.app_name)
+ "\r\nAPP版本号: V" + BuildConfig.VERSION_NAME
+ "\r\nMCU版本号: " + Constants.MCU_VERSION_NUMBER
+ "\r\n中央服务器系列"
+ "\r\n发布日期:" + BuildConfig.BUILD_TIME;
if (android.os.Build.VERSION.SDK_INT >= 26) { //android8.0 = 26
str = str + " android8.1 , rk3368";// rk3368 android8.1
} else if (android.os.Build.VERSION.SDK_INT >= 24) { //android7.0 = 24
str = str + " android7.1 , rk3128";//rk3128 android7.1
} else {
str = str + " android4.2 , a20";// a20 android4.2
}
return str;
}
/**
* 将字符串转为日期类型
*
* @param sdate
* @return
*/
public static Date toDate(String sdate) {
try {
return dateFormater.get().parse(sdate);
} catch (ParseException e) {
return null;
}
}
/**
* 以友好的方式显示时间
*
* @param sdate
* @return
*/
public static String friendly_time(String sdate) {
Date time = toDate(sdate);
if (time == null) {
return "Unknown";
}
String ftime = "";
Calendar cal = Calendar.getInstance();
// 判断是否是同一天
String curDate = dateFormater2.get().format(cal.getTime());
String paramDate = dateFormater2.get().format(time);
if (curDate.equals(paramDate)) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
return ftime;
}
long lt = time.getTime() / 86400000;
long ct = cal.getTimeInMillis() / 86400000;
int days = (int) (ct - lt);
if (days == 0) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
} else if (days == 1) {
ftime = "昨天";
} else if (days == 2) {
ftime = "前天";
} else if (days > 2 && days <= 10) {
ftime = days + "天前";
} else if (days > 10) {
ftime = dateFormater2.get().format(time);
}
return ftime;
}
/**
* 判断给定字符串时间是否为今日
*
* @param sdate
* @return boolean
*/
public static boolean isToday(String sdate) {
boolean b = false;
Date time = toDate(sdate);
Date today = new Date();
if (time != null) {
String nowDate = dateFormater2.get().format(today);
String timeDate = dateFormater2.get().format(time);
if (nowDate.equals(timeDate)) {
b = true;
}
}
return b;
}
/**
* 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
*
* @param input
* @return boolean
*/
public static boolean isEmpty(String input) {
if (input == null || "".equals(input))
return true;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
return false;
}
}
return true;
}
public static boolean notEmpty(String input) {
if (input != null && input.length() > 0
&& !input.equals("null") && !input.equals("NULL")) {
return true;
}
return false;
}
public static boolean listNotEmpty(List list) {
if (list != null && list.size() > 0) {
return true;
}
return false;
}
/**
* 判断是不是一个合法的银行卡
*
* @param cardNum
* @return
*/
public static boolean isCardNum(String cardNum) {
if (cardNum == null || cardNum.trim().length() == 0)
return false;
return backCard.matcher(cardNum).matches();
}
/**
* 判断是不是一个合法的电子邮件地址
*
* @param email
* @return
*/
public static boolean isEmail(String email) {
if (email == null || email.trim().length() == 0)
return false;
return emailer.matcher(email).matches();
}
/**
* 字符串转整数
*
* @param str
* @param defValue
* @return
*/
public static int toInt(String str, int defValue) {
try {
return Integer.parseInt(str);
} catch (Exception e) {
}
return defValue;
}
/**
* 检查是否是金额(12.00)
*
* @param phone
* @return
*/
public static boolean checkNumEx(String phone) {
Pattern pattern = Pattern.compile("^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$");
Matcher match = pattern.matcher(phone);
return match.matches();
}
/**
* 对象转整数
*
* @param obj
* @return 转换异常返回 0
*/
public static int toInt(Object obj) {
if (obj == null)
return 0;
return toInt(obj.toString(), 0);
}
/**
* 对象转整数
*
* @param obj
* @return 转换异常返回 0
*/
public static long toLong(String obj) {
try {
return Long.parseLong(obj);
} catch (Exception e) {
}
return 0;
}
/**
* 字符串转布尔值
*
* @param b
* @return 转换异常返回 false
*/
public static boolean toBool(String b) {
try {
return Boolean.parseBoolean(b);
} catch (Exception e) {
}
return false;
}
/**
* 是否是英文字
*
* @param c
* @return
*/
public static boolean isLetter(char c) {
int k = 0x80;
return c / k == 0 ? true : false;
}
/**
* 是否是英文字
*
* @return
*/
public static boolean isEnglish(String str) {
if (!notEmpty(str)) return false;
return str.matches("[a-zA-Z]+");
}
/**
* 判断一个字符串是否包含某个字符
*
* @return
*/
public static boolean isHaveStr(String old, String s) {
if (!notEmpty(old) || !notEmpty(s)) return false;
return old.contains(s);
}
/**
* 获取英文字长度
*
* @param s
* @return
*/
public static int length(String s) {
if (s == null)
return 0;
char[] c = s.toCharArray();
int len = 0;
int length = c.length;
for (int i = 0; i < length; i++) {
len++;
if (!isLetter(c[i])) {
len++;
}
}
return len;
}
/**
* 去掉html标签
*
* @param htmlStr
* @return
*/
public static String delHTMLTag(String htmlStr) {
String regEx_script = "