zoukankan      html  css  js  c++  java
  • 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 != '	' && c != '
    ' && c != '
    ') {
    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 }
  • 相关阅读:
    我在ZZ这八年
    安装JBpm
    用程序实现HTTP压缩和缓存
    (转)WPF Custom Control Dependency Property Gotcha
    开发一个Word的代码高亮插件
    (转)Getting rid of the DSL model explorer
    (翻译)LearnVSXNow! #16 创建简单的编辑器2
    对 COM 组件的调用返回了错误 HRESULT E_FAIL。
    Word中使用代码高亮插件
    使用HttpWebRequest post数据时要注意UrlEncode
  • 原文地址:https://www.cnblogs.com/androidsj/p/4762283.html
Copyright © 2011-2022 走看看