123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- package com.mylove.okhttp;
- import android.content.Context;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.text.SimpleDateFormat;
- import java.util.Collection;
- import java.util.Date;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 字符串工具类
- */
- class FormatUtil {
- static final String EMPTY = "";
- /**
- * 判断字符串是否为空
- *
- * @param str
- * @return true 不为空, false 为空
- */
- static boolean isNotEmpty(String str) {
- return str != null && !"null".equals(str) && str.trim().length() != 0;
- }
- /**
- * 判断字符串是否为空
- *
- * @param str
- * @return true 为空,false 不为空
- */
- static boolean isEmpty(String str) {
- return str == null || "null".equals(str) || str.trim().length() == 0;
- }
- static final SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm", Locale.CHINA);
- static String getCurrentTime() {
- return dateformat.format(new Date());
- }
- static final SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
- static String formatDateTime(long millseconds) {
- return sdformat.format(new Date(millseconds));
- }
- static String getCurrentDateTime() {
- return sdformat.format(new Date());
- }
- /**
- * 判断集合是否为空
- */
- static <T> boolean isCollectionsNotEmpty(Collection<T> collection) {
- return collection != null && collection.size() > 0;
- }
- /**
- * 判断MAP是否为空
- */
- static <K, V> boolean isMapNotEmpty(Map<K, V> map) {
- return map != null && map.size() > 0;
- }
- /**
- * 判断List是否为空
- */
- static boolean isListEmpty(List<?> array) {
- return array != null && array.size() == 0;
- }
- /**
- * 判断JSON数组是否为空
- */
- static boolean isJSONArrayEmpty(JSONArray array) {
- return array == null || array.length() == 0;
- }
- static boolean isObjectNotNull(Object object) {
- if (object != null && object.getClass().isArray()) {
- // 如果是数组类型
- throw new UnsupportedOperationException("isObjectNotNull not supported operation :" + object);
- }
- return object != null;
- }
- /**
- * 判断JSON数据不空为
- */
- static boolean isJSONArrayNotEmpty(JSONArray array) {
- return array != null && array.length() > 0;
- }
- /**
- * 判断JSON数组是否为空
- */
- static boolean isJSONObjectEmpty(JSONObject object) {
- return object == null || object.length() == 0;
- }
- /**
- * 判断JSON数据不空为
- */
- static boolean isJSONObjectNotEmpty(JSONObject object) {
- return object != null && object.length() > 0;
- }
- static boolean isIntArrayNotEmpty(int[] array) {
- return array != null && array.length > 0;
- }
- /**
- * 判断List数据不空为
- */
- static boolean isListNotEmpty(List<?> array) {
- return array != null && array.size() > 0;
- }
- /**
- * 判断long数组不为空
- *
- * @param array
- * @return
- */
- static boolean isLongArrayNotEmpty(long[] array) {
- return array != null && array.length > 0;
- }
- /**
- * 判断float数组不为空
- *
- * @param array
- * @return
- */
- static boolean isFloatArrayNotEmpty(float[] array) {
- return array != null && array.length > 0;
- }
- /**
- * 判断double数组不为空
- *
- * @param array
- * @return
- */
- static boolean isDoubleArrayNotEmpty(double[] array) {
- return array != null && array.length > 0;
- }
- /**
- * 该方法主要使用正则表达式来判断字符串中是否包含字母
- *
- * @param cardNum
- * @return 返回是否包含
- */
- static boolean isJudge(String cardNum) {
- String regex = ".*[a-zA-Z]+.*";
- Matcher m = Pattern.compile(regex).matcher(cardNum);
- return m.matches();
- }
- static boolean isNotBlank(String str) {
- return (str != null) && (str.length() != 0);
- }
- static boolean isBlank(String str) {
- return (str == null) || (str.length() == 0);
- }
- static boolean isNotTrimBlank(String str) {
- return (str != null) && (str.trim().length() != 0);
- }
- static boolean isTrimBlank(String str) {
- return (str == null) || (str.trim().length() == 0);
- }
- /**
- * 判断是否是身份证
- *
- * @param idNo
- * @return
- */
- static boolean isIdNo(String idNo) {
- // if (isTrimBlank(idNo))
- // {
- // return false;
- // }
- // Pattern p = Pattern.compile("^([1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3})|([1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[X,x]))$");
- // Matcher matcher = p.matcher(idNo);
- // return matcher.find();
- if (isTrimBlank(idNo)) {
- return false;
- }
- Pattern p = Pattern.compile("^([1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3})|([1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[X,x]))$");
- Matcher matcher = p.matcher(idNo);
- return !matcher.find();
- }
- /**
- * 判断是否为手机号
- *
- * @param mobiles
- * @return
- */
- static boolean isNotMobileNO(String mobiles) {
- // Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
- Pattern p = Pattern.compile("^((1[358][0-9])|(14[57])|(17[0678]))\\d{8}$");
- Matcher m = p.matcher(mobiles);
- return !m.matches();
- }
- /**
- * 判断是否为邮箱号
- *
- * @param email
- * @return
- */
- static boolean isEmail(String email) {
- if (isTrimBlank(email)) {
- return false;
- }
- String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
- Pattern p = Pattern.compile(str);
- Matcher m = p.matcher(email);
- return m.matches();
- }
- /**
- * 在HTML特殊字符的处理
- *
- * @param source
- * @return
- */
- static String htmlEscapeCharsToString(String source) {
- return FormatUtil.isEmpty(source) ? source : source.replaceAll("<", "<")
- .replaceAll(">", ">")
- .replaceAll("&", "&")
- .replaceAll(""", "\"")
- .replaceAll("©", "©")
- .replaceAll("¥", "¥")
- .replaceAll("÷", "÷")
- .replaceAll("×", "×")
- .replaceAll("®", "®")
- .replaceAll("§", "§")
- .replaceAll("£", "£")
- .replaceAll("¢", "¢");
- }
- /**
- * 验证用户名是否合法
- *
- * @param id
- * @return
- */
- static boolean isNotUserName(String id) {
- if (isTrimBlank(id)) {
- return false;
- }
- // 字母开头,由字母,数字和下划线组成的长度为2到16的字符串
- Pattern p = Pattern.compile("^[a-zA-Z0-9_-]{2,16}$");
- Matcher m = p.matcher(id);
- return !m.find();
- }
- static boolean isNotPassWord(String password) {
- if (isTrimBlank(password)) {
- return false;
- }
- // 就是以大小写字母开头,由大小写字母,数字和下划线组成的长度为6到18的字符串
- Pattern p = Pattern.compile("^[a-zA-Z0-9_]{6,18}$");
- Matcher m = p.matcher(password);
- return !m.find();
- }
- /**
- * 判断银行卡号是否合法
- *
- * @param bankCard
- * @return
- */
- static boolean isNotBank(String bankCard) {
- if (isTrimBlank(bankCard)) {
- return false;
- }
- // 一共16或19位,都是数字。
- Pattern p = Pattern.compile("^\\d{16}$|^\\d{19}$");
- Matcher m = p.matcher(bankCard);
- return !m.find();
- }
- /**
- * @param context
- * @param resId
- * @param str
- * @return
- */
- static String isStringFormat(Context context, int resId, String str) {
- return String.format(context.getResources().getString(resId), str);
- }
- /**
- * 从Raw文件中读取
- *
- * @param context
- * @param resId
- * @return
- */
- static String getFromRaw(Context context, int resId) {
- try {
- InputStreamReader inputReader = new InputStreamReader(context.getResources().openRawResource(resId));
- BufferedReader bufReader = new BufferedReader(inputReader);
- String line = "";
- StringBuilder Result = new StringBuilder();
- while ((line = bufReader.readLine()) != null)
- Result.append(line);
- return Result.toString();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- // 直接从assets读取
- static String getFromAssets(Context context, String fileName) {
- try {
- InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
- BufferedReader bufReader = new BufferedReader(inputReader);
- String line = "";
- StringBuilder Result = new StringBuilder();
- while ((line = bufReader.readLine()) != null) {
- Result.append(line);
- }
- return Result.toString();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
|