StringUtils 开发中常用的字符串格式

import java.io.UnsupportedEncodingException;
002 import java.security.MessageDigest;
003 import java.security.NoSuchAlgorithmException;
004 import java.text.ParseException;
005 import java.text.SimpleDateFormat;
006 import java.util.Date;
007 import java.util.regex.Pattern;
008  
009 import android.app.Activity;
010 import android.content.Context;
011 import android.content.pm.ApplicationInfo;
012 import android.content.pm.PackageManager;
013 import android.content.pm.PackageManager.NameNotFoundException;
014 import android.os.Bundle;
015 import android.telephony.TelephonyManager;
016  
017 /**
018  * 字符串操作工具包
019  */
020 public class StringUtils {
021     private final static Pattern emailer = Pattern
022             .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
023     private final static Pattern phone = Pattern
024             .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
025  
026     private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
027         @Override
028         protected SimpleDateFormat initialValue() {
029             return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
030         }
031     };
032  
033     private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
034         @Override
035         protected SimpleDateFormat initialValue() {
036             return new SimpleDateFormat("yyyy-MM-dd");
037         }
038     };
039  
040     /**
041      * 返回当前系统时间
042      */
043     public static String getDataTime(String format) {
044         SimpleDateFormat df = new SimpleDateFormat(format);
045         return df.format(new Date());
046     }
047  
048     /**
049      * 返回当前系统时间
050      */
051     public static String getDataTime() {
052         return getDataTime("HH:mm");
053     }
054  
055     /**
056      * 毫秒值转换为mm:ss
057      
058      * @author kymjs
059      * @param ms
060      */
061     public static String timeFormat(int ms) {
062         StringBuilder time = new StringBuilder();
063         time.delete(0, time.length());
064         ms /= 1000;
065         int s = ms % 60;
066         int min = ms / 60;
067         if (min < 10) {
068             time.append(0);
069         }
070         time.append(min).append(":");
071         if (s < 10) {
072             time.append(0);
073         }
074         time.append(s);
075         return time.toString();
076     }
077  
078     /**
079      * 将字符串转位日期类型
080      
081      * @return
082      */
083     public static Date toDate(String sdate) {
084         try {
085             return dateFormater.get().parse(sdate);
086         catch (ParseException e) {
087             return null;
088         }
089     }
090  
091     /**
092      * 判断给定字符串时间是否为今日
093      
094      * @param sdate
095      * @return boolean
096      */
097     public static boolean isToday(String sdate) {
098         boolean b = false;
099         Date time = toDate(sdate);
100         Date today = new Date();
101         if (time != null) {
102             String nowDate = dateFormater2.get().format(today);
103             String timeDate = dateFormater2.get().format(time);
104             if (nowDate.equals(timeDate)) {
105                 b = true;
106             }
107         }
108         return b;
109     }
110  
111     /**
112      * 判断给定字符串是否空白串 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
113      */
114     public static boolean isEmpty(String input) {
115         if (input == null || "".equals(input))
116             return true;
117  
118         for (int i = 0; i < input.length(); i++) {
119             char c = input.charAt(i);
120             if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
121                 return false;
122             }
123         }
124         return true;
125     }
126  
127     /**
128      * 判断是不是一个合法的电子邮件地址
129      */
130     public static boolean isEmail(String email) {
131         if (email == null || email.trim().length() == 0)
132             return false;
133         return emailer.matcher(email).matches();
134     }
135  
136     /**
137      * 判断是不是一个合法的手机号码
138      */
139     public static boolean isPhone(String phoneNum) {
140         if (phoneNum == null || phoneNum.trim().length() == 0)
141             return false;
142         return phone.matcher(phoneNum).matches();
143     }
144  
145     /**
146      * 字符串转整数
147      
148      * @param str
149      * @param defValue
150      * @return
151      */
152     public static int toInt(String str, int defValue) {
153         try {
154             return Integer.parseInt(str);
155         catch (Exception e) {
156         }
157         return defValue;
158     }
159  
160     /**
161      * 对象转整
162      
163      * @param obj
164      * @return 转换异常返回 0
165      */
166     public static int toInt(Object obj) {
167         if (obj == null)
168             return 0;
169         return toInt(obj.toString(), 0);
170     }
171  
172     /**
173      * String转long
174      
175      * @param obj
176      * @return 转换异常返回 0
177      */
178     public static long toLong(String obj) {
179         try {
180             return Long.parseLong(obj);
181         catch (Exception e) {
182         }
183         return 0;
184     }
185  
186     /**
187      * String转double
188      
189      * @param obj
190      * @return 转换异常返回 0
191      */
192     public static double toDouble(String obj) {
193         try {
194             return Double.parseDouble(obj);
195         catch (Exception e) {
196         }
197         return 0D;
198     }
199  
200     /**
201      * 字符串转布尔
202      
203      * @param b
204      * @return 转换异常返回 false
205      */
206     public static boolean toBool(String b) {
207         try {
208             return Boolean.parseBoolean(b);
209         catch (Exception e) {
210         }
211         return false;
212     }
213  
214     /**
215      * 判断一个字符串是不是数字
216      */
217     public static boolean isNumber(String str) {
218         try {
219             Integer.parseInt(str);
220         catch (Exception e) {
221             return false;
222         }
223         return true;
224     }
225  
226     /**
227      * 获取AppKey
228      */
229     public static String getMetaValue(Context context, String metaKey) {
230         Bundle metaData = null;
231         String apiKey = null;
232         if (context == null || metaKey == null) {
233             return null;
234         }
235         try {
236             ApplicationInfo ai = context.getPackageManager()
237                     .getApplicationInfo(context.getPackageName(),
238                             PackageManager.GET_META_DATA);
239             if (null != ai) {
240                 metaData = ai.metaData;
241             }
242             if (null != metaData) {
243                 apiKey = metaData.getString(metaKey);
244             }
245         catch (NameNotFoundException e) {
246  
247         }
248         return apiKey;
249     }
250  
251     /**
252      * 获取手机IMEI码
253      */
254     public static String getPhoneIMEI(Activity aty) {
255         TelephonyManager tm = (TelephonyManager) aty
256                 .getSystemService(Activity.TELEPHONY_SERVICE);
257         return tm.getDeviceId();
258     }
259  
260     /**
261      * MD5加密
262      */
263     public static String md5(String string) {
264         byte[] hash;
265         try {
266             hash = MessageDigest.getInstance("MD5").digest(
267                     string.getBytes("UTF-8"));
268         catch (NoSuchAlgorithmException e) {
269             throw new RuntimeException("Huh, MD5 should be supported?", e);
270         catch (UnsupportedEncodingException e) {
271             throw new RuntimeException("Huh, UTF-8 should be supported?", e);
272         }
273  
274         StringBuilder hex = new StringBuilder(hash.length * 2);
275         for (byte b : hash) {
276             if ((b & 0xFF) < 0x10)
277                 hex.append("0");
278             hex.append(Integer.toHexString(b & 0xFF));
279         }
280         return hex.toString();
281     }
282  
283     /**
284      * KJ加密
285      */
286     public static String KJencrypt(String str) {
287         char[] cstr = str.toCharArray();
288         StringBuilder hex = new StringBuilder();
289         for (char c : cstr) {
290             hex.append((char) (c + 5));
291         }
292         return hex.toString();
293     }
294  
295     /**
296      * KJ解密
297      */
298     public static String KJdecipher(String str) {
299         char[] cstr = str.toCharArray();
300         StringBuilder hex = new StringBuilder();
301         for (char c : cstr) {
302             hex.append((char) (c - 5));
303         }
304         return hex.toString();
305     }
306 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值