package com.xiaoyun.org.util;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern; /**
* 字符串操作工具包
*
* @author liux (http://my.oschina.net/liux)
* @version 1.0
* @created 2012-3-21
*/ public class StringUtils {
private final static Pattern emailer = Pattern
.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
private final static Pattern phones = Pattern
.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); // private final static SimpleDateFormat dateFormater = new
// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// private final static SimpleDateFormat dateFormater2 = new
// SimpleDateFormat("yyyy-MM-dd"); private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}; private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
}; /**
* 将字符串转位日期类型
*
* @param sdate
* @return
*/
public static Date toDate(String sdate) {
try {
return dateFormater.get().parse(sdate);
} catch (ParseException e) {
return null;
}
} /**
* 以友好的方式显示时间
*
* @param sdate
* @return
*/
public static String friendly_time(String sdate) {
Date time = toDate(sdate);
if (time == null) {
return "Unknown";
}
String ftime = "";
Calendar cal = Calendar.getInstance(); // 判断是否是同一天
String curDate = dateFormater2.get().format(cal.getTime());
String paramDate = dateFormater2.get().format(time);
if (curDate.equals(paramDate)) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
return ftime;
} long lt = time.getTime() / 86400000;
long ct = cal.getTimeInMillis() / 86400000;
int days = (int) (ct - lt);
if (days == 0) {
int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
if (hour == 0)
ftime = Math.max(
(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
+ "分钟前";
else
ftime = hour + "小时前";
} else if (days == 1) {
ftime = "昨天";
} else if (days == 2) {
ftime = "前天";
} else if (days > 2 && days <= 10) {
ftime = days + "天前";
} else if (days > 10) {
ftime = dateFormater2.get().format(time);
}
return ftime;
} /**
* 判断给定字符串时间是否为今日
*
* @param sdate
* @return boolean
*/
public static boolean isToday(String sdate) {
boolean b = false;
Date time = toDate(sdate);
Date today = new Date();
if (time != null) {
String nowDate = dateFormater2.get().format(today);
String timeDate = dateFormater2.get().format(time);
if (nowDate.equals(timeDate)) {
b = true;
}
}
return b;
} /**
* 返回long类型的今天的日期
*
* @return
*/
public static long getToday() {
Calendar cal = Calendar.getInstance();
String curDate = dateFormater2.get().format(cal.getTime());
curDate = curDate.replace("-", "");
return Long.parseLong(curDate);
} /**
* 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
*
* @param input
* @return boolean
*/
public static boolean isEmpty(String input) {
if (input == null || "".equals(input))
return true; for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
return false;
}
}
return true;
} /**
* 判断是不是一个合法的电子邮件地址
*
* @param email
* @return
*/
public static boolean isEmail(String email) {
if (email == null || email.trim().length() == 0)
return false;
return emailer.matcher(email).matches();
} /**
* 判断是不是一个合法的手机号
*
* @param phone
* @return
*/
public static boolean isPhone(String phone) {
if (phone == null || phone.trim().length() == 0)
return false;
return phones.matcher(phone).matches();
} /**
* 字符串转整数
*
* @param str
* @param defValue
* @return
*/
public static int toInt(String str, int defValue) {
try {
return Integer.parseInt(str);
} catch (Exception e) {
}
return defValue;
} /**
* 对象转整数
*
* @param obj
* @return 转换异常返回 0
*/
public static int toInt(Object obj) {
if (obj == null)
return 0;
return toInt(obj.toString(), 0);
} /**
* 对象转整数
*
* @param obj
* @return 转换异常返回 0
*/
public static long toLong(String obj) {
try {
return Long.parseLong(obj);
} catch (Exception e) {
}
return 0;
} /**
* 字符串转布尔值
*
* @param b
* @return 转换异常返回 false
*/
public static boolean toBool(String b) {
try {
return Boolean.parseBoolean(b);
} catch (Exception e) {
}
return false;
} /**
* 将一个InputStream流转换成字符串
*
* @param is
* @return
*/
public static String toConvertString(InputStream is) {
StringBuffer res = new StringBuffer();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader read = new BufferedReader(isr);
try {
String line;
line = read.readLine();
while (line != null) {
res.append(line);
line = read.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != isr) {
isr.close();
isr.close();
}
if (null != read) {
read.close();
read = null;
}
if (null != is) {
is.close();
is = null;
}
} catch (IOException e) {
}
}
return res.toString();
} private static long lastClickTime;
public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if ( 0 < timeD && timeD < 2000) {
return true;
}
lastClickTime = time;
return false;
}
}

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

  1. PHP 判断手机号归属地 和 运营商的免费接口

    在项目开发的时候,需要去查询又一批手机号或者固话的具体信息(归属地 运营商) 就需要写一个脚本,来批量请求接口来得到我们想要的数据 学习源头:https://blog.csdn.net/shaerdo ...

  2. js获取当前时间显示在页面上

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. jsp界面动态时间显示

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  4. PHP比较全的友好的时间显示,比如‘刚刚’,'几秒前'等

    分享一个php友好的比较完成的时间格式化函数,包括‘刚刚’,'几秒之前',‘几分钟前’,'几小时前',几天前,几周前,几个月前等.调用方式很简单,是从ThinkSNS 里面拿出来的. /** * 友好 ...

  5. 将时间显示为“刚刚”“n分钟/小时前”等

    在很多场合为了显示出信息的及时性,一般会将时间显示成“刚刚”,“5分钟前”,“3小时前”等,而不是直接将时间打印出来.比如微博,SNS类应用就最长用到这个功能.而一般存储在数据库中的时间格式为 Uni ...

  6. phpcms v9 搜索结果列表页时间显示1970问题解决方案

    对于喜欢用phpcms v9 的小伙伴来说,在调用时间时,总会出现时间1970这样的问题,对于这个问题,网上的说法很多,内容页时间显示通常不会问题,搜索结果页就不行了,通过总结,发现使用{format ...

  7. 电脑时间显示秒 win10电脑显示农历

    win10电脑时间显示秒 显示农历. Win10怎样让任务栏时间显示秒_百度经验 win10电脑显示农历 网上搜到的不管用. 直接下载win10万年历.我下载的人生日历. 最烦广告, 还有一些流氓行为 ...

  8. GUI带有右键菜单,带有时间显示的

    %带有右键菜单的GUI figure('Menubar','none'); h = uicontextmenu; uimenu(h,'Label','A'); uimenu(h,'Label','B' ...

  9. MySQL 5.7 时间显示修改(log_timestamps UTC)

    https://blog.csdn.net/leshami/article/details/78952842 在MySQL 5.7版本中,日志记录时间发生了变化,使用了UTC方式来记录日志时间,也就是 ...

随机推荐

  1. webstorm 破解方式

    注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key.php 点击:Acti ...

  2. Macro definition of snprintf conflicts with Standard Library function declaration

    Macro definition of snprintf conflicts with Standard Library function declaration 即将此处的宏定义注释掉,因为在VS2 ...

  3. ParallelProgramming-多消费者,多生产者同时运行并行

    在上一篇文章演示了并行的流水线操作(生产者和消费者并行同时执行),C#是通过BlockingCollection这个线程安全的对象作为Buffer,并且结合Task来实现的.但是上一篇文章有个缺陷,在 ...

  4. vue - rimraf

    rimraf 包的作用:以包的形式包装rm -rf命令,用来删除文件和文件夹的,不管文件夹是否为空,都可删除 const rimraf = require('rimraf'); rimraf('./t ...

  5. unity Chan!下载

    http://unity-chan.com/download/datadownload.html

  6. 解决Odoo访问fonts.googleapis.com导致速度慢的问题

    Odoo中有些css文件引用了谷歌字体,但因为谷歌服务器被墙,导致部分页面加载受影响. 解决方法如下: 360网站卫士常用前端公共库CDN服务把谷歌字体库都存到它的CDN上了,因此我们只需把样式表中谷 ...

  7. requireJS目录

    前言 对于像我这种requireJS初学者而言,requireJS最难理解的部分应该是它的路径问题.晚上随便折腾了一下,算是稍微理清了这个目录问题吧. requireJS学习网址:requireJS中 ...

  8. 纪念我人生中第一个merge into语句

    做按组织关系汇总功能时,当数据量特别大,或者汇总组织特别多时,运行效率特别低,于是使用了merge into语句. 代码如下: public void updateInsertData(DataSet ...

  9. RabbitMQ快速入门python教程

    摘要:HelloWorld 简介 RabbitMQ:接受消息再传递消息,可以视为一个“邮局”.发送者和接受者通过队列来进行交互,队列的大小可以视为无限的,多个发送者可以发生给一个队列,多个接收者也可以 ...

  10. mysql group by 与order by的实例分析(mysql分组统计后最大值)

    CREATE TABLE `test` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) CHARACTER SET latin1 DEFAULT NULL, `c ...