FormatUtil.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package com.mylove.okhttp;
  2. import android.content.Context;
  3. import org.json.JSONArray;
  4. import org.json.JSONObject;
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Collection;
  9. import java.util.Date;
  10. import java.util.List;
  11. import java.util.Locale;
  12. import java.util.Map;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. /**
  16. * 字符串工具类
  17. */
  18. class FormatUtil {
  19. static final String EMPTY = "";
  20. /**
  21. * 判断字符串是否为空
  22. *
  23. * @param str
  24. * @return true 不为空, false 为空
  25. */
  26. static boolean isNotEmpty(String str) {
  27. return str != null && !"null".equals(str) && str.trim().length() != 0;
  28. }
  29. /**
  30. * 判断字符串是否为空
  31. *
  32. * @param str
  33. * @return true 为空,false 不为空
  34. */
  35. static boolean isEmpty(String str) {
  36. return str == null || "null".equals(str) || str.trim().length() == 0;
  37. }
  38. static final SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm", Locale.CHINA);
  39. static String getCurrentTime() {
  40. return dateformat.format(new Date());
  41. }
  42. static final SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
  43. static String formatDateTime(long millseconds) {
  44. return sdformat.format(new Date(millseconds));
  45. }
  46. static String getCurrentDateTime() {
  47. return sdformat.format(new Date());
  48. }
  49. /**
  50. * 判断集合是否为空
  51. */
  52. static <T> boolean isCollectionsNotEmpty(Collection<T> collection) {
  53. return collection != null && collection.size() > 0;
  54. }
  55. /**
  56. * 判断MAP是否为空
  57. */
  58. static <K, V> boolean isMapNotEmpty(Map<K, V> map) {
  59. return map != null && map.size() > 0;
  60. }
  61. /**
  62. * 判断List是否为空
  63. */
  64. static boolean isListEmpty(List<?> array) {
  65. return array != null && array.size() == 0;
  66. }
  67. /**
  68. * 判断JSON数组是否为空
  69. */
  70. static boolean isJSONArrayEmpty(JSONArray array) {
  71. return array == null || array.length() == 0;
  72. }
  73. static boolean isObjectNotNull(Object object) {
  74. if (object != null && object.getClass().isArray()) {
  75. // 如果是数组类型
  76. throw new UnsupportedOperationException("isObjectNotNull not supported operation :" + object);
  77. }
  78. return object != null;
  79. }
  80. /**
  81. * 判断JSON数据不空为
  82. */
  83. static boolean isJSONArrayNotEmpty(JSONArray array) {
  84. return array != null && array.length() > 0;
  85. }
  86. /**
  87. * 判断JSON数组是否为空
  88. */
  89. static boolean isJSONObjectEmpty(JSONObject object) {
  90. return object == null || object.length() == 0;
  91. }
  92. /**
  93. * 判断JSON数据不空为
  94. */
  95. static boolean isJSONObjectNotEmpty(JSONObject object) {
  96. return object != null && object.length() > 0;
  97. }
  98. static boolean isIntArrayNotEmpty(int[] array) {
  99. return array != null && array.length > 0;
  100. }
  101. /**
  102. * 判断List数据不空为
  103. */
  104. static boolean isListNotEmpty(List<?> array) {
  105. return array != null && array.size() > 0;
  106. }
  107. /**
  108. * 判断long数组不为空
  109. *
  110. * @param array
  111. * @return
  112. */
  113. static boolean isLongArrayNotEmpty(long[] array) {
  114. return array != null && array.length > 0;
  115. }
  116. /**
  117. * 判断float数组不为空
  118. *
  119. * @param array
  120. * @return
  121. */
  122. static boolean isFloatArrayNotEmpty(float[] array) {
  123. return array != null && array.length > 0;
  124. }
  125. /**
  126. * 判断double数组不为空
  127. *
  128. * @param array
  129. * @return
  130. */
  131. static boolean isDoubleArrayNotEmpty(double[] array) {
  132. return array != null && array.length > 0;
  133. }
  134. /**
  135. * 该方法主要使用正则表达式来判断字符串中是否包含字母
  136. *
  137. * @param cardNum
  138. * @return 返回是否包含
  139. */
  140. static boolean isJudge(String cardNum) {
  141. String regex = ".*[a-zA-Z]+.*";
  142. Matcher m = Pattern.compile(regex).matcher(cardNum);
  143. return m.matches();
  144. }
  145. static boolean isNotBlank(String str) {
  146. return (str != null) && (str.length() != 0);
  147. }
  148. static boolean isBlank(String str) {
  149. return (str == null) || (str.length() == 0);
  150. }
  151. static boolean isNotTrimBlank(String str) {
  152. return (str != null) && (str.trim().length() != 0);
  153. }
  154. static boolean isTrimBlank(String str) {
  155. return (str == null) || (str.trim().length() == 0);
  156. }
  157. /**
  158. * 判断是否是身份证
  159. *
  160. * @param idNo
  161. * @return
  162. */
  163. static boolean isIdNo(String idNo) {
  164. // if (isTrimBlank(idNo))
  165. // {
  166. // return false;
  167. // }
  168. // 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]))$");
  169. // Matcher matcher = p.matcher(idNo);
  170. // return matcher.find();
  171. if (isTrimBlank(idNo)) {
  172. return false;
  173. }
  174. 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]))$");
  175. Matcher matcher = p.matcher(idNo);
  176. return !matcher.find();
  177. }
  178. /**
  179. * 判断是否为手机号
  180. *
  181. * @param mobiles
  182. * @return
  183. */
  184. static boolean isNotMobileNO(String mobiles) {
  185. // Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
  186. Pattern p = Pattern.compile("^((1[358][0-9])|(14[57])|(17[0678]))\\d{8}$");
  187. Matcher m = p.matcher(mobiles);
  188. return !m.matches();
  189. }
  190. /**
  191. * 判断是否为邮箱号
  192. *
  193. * @param email
  194. * @return
  195. */
  196. static boolean isEmail(String email) {
  197. if (isTrimBlank(email)) {
  198. return false;
  199. }
  200. 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})?$";
  201. Pattern p = Pattern.compile(str);
  202. Matcher m = p.matcher(email);
  203. return m.matches();
  204. }
  205. /**
  206. * 在HTML特殊字符的处理
  207. *
  208. * @param source
  209. * @return
  210. */
  211. static String htmlEscapeCharsToString(String source) {
  212. return FormatUtil.isEmpty(source) ? source : source.replaceAll("&lt;", "<")
  213. .replaceAll("&gt;", ">")
  214. .replaceAll("&amp;", "&")
  215. .replaceAll("&quot;", "\"")
  216. .replaceAll("&copy;", "©")
  217. .replaceAll("&yen;", "¥")
  218. .replaceAll("&divide;", "÷")
  219. .replaceAll("&times;", "×")
  220. .replaceAll("&reg;", "®")
  221. .replaceAll("&sect;", "§")
  222. .replaceAll("&pound;", "£")
  223. .replaceAll("&cent;", "¢");
  224. }
  225. /**
  226. * 验证用户名是否合法
  227. *
  228. * @param id
  229. * @return
  230. */
  231. static boolean isNotUserName(String id) {
  232. if (isTrimBlank(id)) {
  233. return false;
  234. }
  235. // 字母开头,由字母,数字和下划线组成的长度为2到16的字符串
  236. Pattern p = Pattern.compile("^[a-zA-Z0-9_-]{2,16}$");
  237. Matcher m = p.matcher(id);
  238. return !m.find();
  239. }
  240. static boolean isNotPassWord(String password) {
  241. if (isTrimBlank(password)) {
  242. return false;
  243. }
  244. // 就是以大小写字母开头,由大小写字母,数字和下划线组成的长度为6到18的字符串
  245. Pattern p = Pattern.compile("^[a-zA-Z0-9_]{6,18}$");
  246. Matcher m = p.matcher(password);
  247. return !m.find();
  248. }
  249. /**
  250. * 判断银行卡号是否合法
  251. *
  252. * @param bankCard
  253. * @return
  254. */
  255. static boolean isNotBank(String bankCard) {
  256. if (isTrimBlank(bankCard)) {
  257. return false;
  258. }
  259. // 一共16或19位,都是数字。
  260. Pattern p = Pattern.compile("^\\d{16}$|^\\d{19}$");
  261. Matcher m = p.matcher(bankCard);
  262. return !m.find();
  263. }
  264. /**
  265. * @param context
  266. * @param resId
  267. * @param str
  268. * @return
  269. */
  270. static String isStringFormat(Context context, int resId, String str) {
  271. return String.format(context.getResources().getString(resId), str);
  272. }
  273. /**
  274. * 从Raw文件中读取
  275. *
  276. * @param context
  277. * @param resId
  278. * @return
  279. */
  280. static String getFromRaw(Context context, int resId) {
  281. try {
  282. InputStreamReader inputReader = new InputStreamReader(context.getResources().openRawResource(resId));
  283. BufferedReader bufReader = new BufferedReader(inputReader);
  284. String line = "";
  285. StringBuilder Result = new StringBuilder();
  286. while ((line = bufReader.readLine()) != null)
  287. Result.append(line);
  288. return Result.toString();
  289. } catch (Exception e) {
  290. e.printStackTrace();
  291. }
  292. return null;
  293. }
  294. // 直接从assets读取
  295. static String getFromAssets(Context context, String fileName) {
  296. try {
  297. InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
  298. BufferedReader bufReader = new BufferedReader(inputReader);
  299. String line = "";
  300. StringBuilder Result = new StringBuilder();
  301. while ((line = bufReader.readLine()) != null) {
  302. Result.append(line);
  303. }
  304. return Result.toString();
  305. } catch (Exception e) {
  306. e.printStackTrace();
  307. }
  308. return null;
  309. }
  310. }