StringUtils 时间显示,判断手机号,电子邮件,是否为今日,是否空白串,字符串转整数,对象转整数 等

  1 package com.xiaoyun.org.util;
  2
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStream;
  6 import java.io.InputStreamReader;
  7 import java.text.ParseException;
  8 import java.text.SimpleDateFormat;
  9 import java.util.Calendar;
 10 import java.util.Date;
 11 import java.util.regex.Pattern;
 12
 13 /**
 14  * 字符串操作工具包
 15  *
 16  * @author liux (http://my.oschina.net/liux)
 17  * @version 1.0
 18  * @created 2012-3-21
 19  */
 20
 21 public class StringUtils {
 22     private final static Pattern emailer = Pattern
 23             .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
 24     private final static Pattern phones = Pattern
 25             .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
 26
 27     // private final static SimpleDateFormat dateFormater = new
 28     // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 29     // private final static SimpleDateFormat dateFormater2 = new
 30     // SimpleDateFormat("yyyy-MM-dd");
 31
 32     private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
 33         @Override
 34         protected SimpleDateFormat initialValue() {
 35             return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 36         }
 37     };
 38
 39     private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
 40         @Override
 41         protected SimpleDateFormat initialValue() {
 42             return new SimpleDateFormat("yyyy-MM-dd");
 43         }
 44     };
 45
 46     /**
 47      * 将字符串转位日期类型
 48      *
 49      * @param sdate
 50      * @return
 51      */
 52     public static Date toDate(String sdate) {
 53         try {
 54             return dateFormater.get().parse(sdate);
 55         } catch (ParseException e) {
 56             return null;
 57         }
 58     }
 59
 60     /**
 61      * 以友好的方式显示时间
 62      *
 63      * @param sdate
 64      * @return
 65      */
 66     public static String friendly_time(String sdate) {
 67         Date time = toDate(sdate);
 68         if (time == null) {
 69             return "Unknown";
 70         }
 71         String ftime = "";
 72         Calendar cal = Calendar.getInstance();
 73
 74         // 判断是否是同一天
 75         String curDate = dateFormater2.get().format(cal.getTime());
 76         String paramDate = dateFormater2.get().format(time);
 77         if (curDate.equals(paramDate)) {
 78             int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
 79             if (hour == 0)
 80                 ftime = Math.max(
 81                         (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
 82                         + "分钟前";
 83             else
 84                 ftime = hour + "小时前";
 85             return ftime;
 86         }
 87
 88         long lt = time.getTime() / 86400000;
 89         long ct = cal.getTimeInMillis() / 86400000;
 90         int days = (int) (ct - lt);
 91         if (days == 0) {
 92             int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
 93             if (hour == 0)
 94                 ftime = Math.max(
 95                         (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
 96                         + "分钟前";
 97             else
 98                 ftime = hour + "小时前";
 99         } else if (days == 1) {
100             ftime = "昨天";
101         } else if (days == 2) {
102             ftime = "前天";
103         } else if (days > 2 && days <= 10) {
104             ftime = days + "天前";
105         } else if (days > 10) {
106             ftime = dateFormater2.get().format(time);
107         }
108         return ftime;
109     }
110
111     /**
112      * 判断给定字符串时间是否为今日
113      *
114      * @param sdate
115      * @return boolean
116      */
117     public static boolean isToday(String sdate) {
118         boolean b = false;
119         Date time = toDate(sdate);
120         Date today = new Date();
121         if (time != null) {
122             String nowDate = dateFormater2.get().format(today);
123             String timeDate = dateFormater2.get().format(time);
124             if (nowDate.equals(timeDate)) {
125                 b = true;
126             }
127         }
128         return b;
129     }
130
131     /**
132      * 返回long类型的今天的日期
133      *
134      * @return
135      */
136     public static long getToday() {
137         Calendar cal = Calendar.getInstance();
138         String curDate = dateFormater2.get().format(cal.getTime());
139         curDate = curDate.replace("-", "");
140         return Long.parseLong(curDate);
141     }
142
143     /**
144      * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
145      *
146      * @param input
147      * @return boolean
148      */
149     public static boolean isEmpty(String input) {
150         if (input == null || "".equals(input))
151             return true;
152
153         for (int i = 0; i < input.length(); i++) {
154             char c = input.charAt(i);
155             if (c != ‘ ‘ && c != ‘\t‘ && c != ‘\r‘ && c != ‘\n‘) {
156                 return false;
157             }
158         }
159         return true;
160     }
161
162     /**
163      * 判断是不是一个合法的电子邮件地址
164      *
165      * @param email
166      * @return
167      */
168     public static boolean isEmail(String email) {
169         if (email == null || email.trim().length() == 0)
170             return false;
171         return emailer.matcher(email).matches();
172     }
173
174     /**
175      * 判断是不是一个合法的手机号
176      *
177      * @param phone
178      * @return
179      */
180     public static boolean isPhone(String phone) {
181         if (phone == null || phone.trim().length() == 0)
182             return false;
183         return phones.matcher(phone).matches();
184     }
185
186     /**
187      * 字符串转整数
188      *
189      * @param str
190      * @param defValue
191      * @return
192      */
193     public static int toInt(String str, int defValue) {
194         try {
195             return Integer.parseInt(str);
196         } catch (Exception e) {
197         }
198         return defValue;
199     }
200
201     /**
202      * 对象转整数
203      *
204      * @param obj
205      * @return 转换异常返回 0
206      */
207     public static int toInt(Object obj) {
208         if (obj == null)
209             return 0;
210         return toInt(obj.toString(), 0);
211     }
212
213     /**
214      * 对象转整数
215      *
216      * @param obj
217      * @return 转换异常返回 0
218      */
219     public static long toLong(String obj) {
220         try {
221             return Long.parseLong(obj);
222         } catch (Exception e) {
223         }
224         return 0;
225     }
226
227     /**
228      * 字符串转布尔值
229      *
230      * @param b
231      * @return 转换异常返回 false
232      */
233     public static boolean toBool(String b) {
234         try {
235             return Boolean.parseBoolean(b);
236         } catch (Exception e) {
237         }
238         return false;
239     }
240
241     /**
242      * 将一个InputStream流转换成字符串
243      *
244      * @param is
245      * @return
246      */
247     public static String toConvertString(InputStream is) {
248         StringBuffer res = new StringBuffer();
249         InputStreamReader isr = new InputStreamReader(is);
250         BufferedReader read = new BufferedReader(isr);
251         try {
252             String line;
253             line = read.readLine();
254             while (line != null) {
255                 res.append(line);
256                 line = read.readLine();
257             }
258         } catch (IOException e) {
259             e.printStackTrace();
260         } finally {
261             try {
262                 if (null != isr) {
263                     isr.close();
264                     isr.close();
265                 }
266                 if (null != read) {
267                     read.close();
268                     read = null;
269                 }
270                 if (null != is) {
271                     is.close();
272                     is = null;
273                 }
274             } catch (IOException e) {
275             }
276         }
277         return res.toString();
278     }
279
280     private static long lastClickTime;
281     public static boolean isFastDoubleClick() {
282         long time = System.currentTimeMillis();
283         long timeD = time - lastClickTime;
284         if ( 0 < timeD && timeD < 2000) {
285             return true;
286         }
287         lastClickTime = time;
288         return false;
289     }
290 }
时间: 2025-01-07 15:16:10

StringUtils 时间显示,判断手机号,电子邮件,是否为今日,是否空白串,字符串转整数,对象转整数 等的相关文章

PHP 判断手机号归属地

最近由于工作需要,要用PHP判断手机号的归属地,方法有很多,最常见的方法是第三方提供的api,常见的api如下: 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443 参数: tel:手机号码 返回:JSON 二.拍拍API API地址: http://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=1585078144

Windows 修改个性化时间显示

我感觉我的时间显示不够人性化.不够个性化 修改注册表 我的系统为Windows 10 下图为我的时间显示 我感觉它不够个性化,人性化,我想自定义一份独特的时间显示格式 修改注册表 Windows键+R键,调出一下窗口,输入regedit 打开注册表 如下图: 定位到  \HKEY_CURRENT_USER\Control Panel\International 在右侧找到sTimeFormat , 修改该值的数值为"现在是 HH:mm:ss" 修改完成后可以即时看到效果 如下图: 修改

Android5.1 - 锁屏界面时间显示

[问题]待机唤醒解锁界面时间显示不全.不论是8寸还是7寸的屏幕都有此问题.时间显示设置为“上午10:30”的时候,最右边的数字0残缺.而时间数字少于4个时,数字不会残缺. [debug]找到相关的配置文件,把文字的大小修改为合适的值即可.在frameworks/base/packages下有2个目录,分别是Keyguard和SystemUI. 查看SystemUI的Android.mk文件LOCAL_STATIC_JAVA_LIBRARIES := Keyguard android-suppor

判断手机号归属运营商

1 /** 2 * 手机号归属运营商查询 3 * @param phone 4 */ 5 public static void mobileOperator(String phone) { 6 // cmcc-中国移动手机号码规则 7 String cmccRegex = "^[1]{1}(([3]{1}[4-9]{1})|([5]{1}[89]{1}))[0-9]{8}$"; 8 // cucc-中国联通手机号码规则 9 String cuccRegex = "^[1]{1

判断手机号,密码的正则表达式

//判断密码6-16位 + (BOOL)validatePassword:(NSString *)password{ NSString *Regex = @"^[a-zA-Z0-9]{5,16}$"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex]; return [emailTest evaluateWithObject:password];

js调用时间显示日期为116年

今天在做一个客户网站的时候,突然发现以前用的调用系统时间的代码突然显示不正常了,时间显示116年,于是百度了一下,原来是 Date().getYear()这个API被废弃了,显示的是自1900年以来所经过的时间.于是果断加了个1900. 原代码如下: <script language=JavaScript> today=new Date(); function initArray(){ this.length=initArray.arguments.length for(var i=0;i&l

NSPredicate判断手机号、邮箱、qq、重名、

#import <Foundation/Foundation.h> @interface NSString (InputCheck) - (BOOL) validateEmail; - (BOOL) validateMobile; - (BOOL) validateqq; - (BOOL) validateRealName; - (BOOL) validateNickName; - (BOOL) validateUserId; @end //--------------------------

过一定时间显示可用控件

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>     <title></title>     <script type="text/ja

一个日期时间显示框的美化风格示例

一个日期时间显示框的美化风格示例,在网页上显示时间的一个美化示例,为时间显示框增加了一个漂亮的外框,这个外框是基于图片来美化的,,虽然现在都不主张用图片来美化了,不过看上去还真是挺漂亮的.www.srcfans.com为大家分享开源源码. 源码下载:一个日期时间显示框的美化风格示例