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. vue第三方ui使用

    举个例子. http://git.oschina.net/tianyong/we-vue 假如要在项目中使用这套ui. npm npm install we-vue -S 直接安装就行了.不需要下载源 ...

  2. JavaScript操作dom总结

    最近一直忙于新项目,真的挺费心的 从产品原型.ui.接口.真心挺费心的.好多地方都不完善!(i want say F word!!) 基础的东西又需要重新看看了! Node Node.NodeType ...

  3. Lucene分词器

    Lucene分析器的基类为Analyzer,Analyzer包含两个核心组件:Tokenizer和 TokenFilter.自定义分析器必须实现Analyzer类的抽象方法createComponen ...

  4. Linux学习笔记 (八)Shell概述

    一.什么是Shell? Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动,挂起,停止甚至是编写一些程序.Shell还是一 ...

  5. TP细节总结1

    TP细节总结1 接收参数尽量使用I函数,代替post.get 更安全! I('变量类型.变量名/修饰符',['默认值'],['过滤方法'],['额外数据源']) 在where 条件处尽量使用 arra ...

  6. MVC你是怎样理解的?

    近期的学术交流会议都在研究MVC,秉着好好学习,天天向上的心理,时不时的就去蹭会.说实话,原来也有看过MVC的一些相关资料,可是仅仅是浅尝辄止.并且,由于先学了三层,所以再看MVC的时候,潜意识里的就 ...

  7. WPF中Auto与*的差别

    Auto 表示自己主动适应显示内容的宽度, 如自己主动适应文本的宽度,文本有多长,控件就显示多长. * 则表示按比例来分配宽度. <ColumnDefinition Width="3* ...

  8. 安装Drupal7.12+Postgresql9.1(Ubuntu Server 12.04)

    怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ OS环境准备 这次是从OS开始安装的.最开始装Ubuntu12.04这里就不再赘述, 唯 ...

  9. EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题

    public static DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate< ...

  10. Phaser实现源代码剖析

    Phaser是一个能够反复利用的同步栅栏.功能上与CyclicBarrier和CountDownLatch相似,只是提供更加灵活的使用方法.也就是说,Phaser的同步模型与它们几乎相同. 一般运用的 ...