StringUtils 时间显示,判断手机号,电子邮件,是否为今日,是否空白串,字符串转整数,对象转整数 等
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 时间显示,判断手机号,电子邮件,是否为今日,是否空白串,字符串转整数,对象转整数 等的更多相关文章
- PHP 判断手机号归属地 和 运营商的免费接口
在项目开发的时候,需要去查询又一批手机号或者固话的具体信息(归属地 运营商) 就需要写一个脚本,来批量请求接口来得到我们想要的数据 学习源头:https://blog.csdn.net/shaerdo ...
- js获取当前时间显示在页面上
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- jsp界面动态时间显示
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- PHP比较全的友好的时间显示,比如‘刚刚’,'几秒前'等
分享一个php友好的比较完成的时间格式化函数,包括‘刚刚’,'几秒之前',‘几分钟前’,'几小时前',几天前,几周前,几个月前等.调用方式很简单,是从ThinkSNS 里面拿出来的. /** * 友好 ...
- 将时间显示为“刚刚”“n分钟/小时前”等
在很多场合为了显示出信息的及时性,一般会将时间显示成“刚刚”,“5分钟前”,“3小时前”等,而不是直接将时间打印出来.比如微博,SNS类应用就最长用到这个功能.而一般存储在数据库中的时间格式为 Uni ...
- phpcms v9 搜索结果列表页时间显示1970问题解决方案
对于喜欢用phpcms v9 的小伙伴来说,在调用时间时,总会出现时间1970这样的问题,对于这个问题,网上的说法很多,内容页时间显示通常不会问题,搜索结果页就不行了,通过总结,发现使用{format ...
- 电脑时间显示秒 win10电脑显示农历
win10电脑时间显示秒 显示农历. Win10怎样让任务栏时间显示秒_百度经验 win10电脑显示农历 网上搜到的不管用. 直接下载win10万年历.我下载的人生日历. 最烦广告, 还有一些流氓行为 ...
- GUI带有右键菜单,带有时间显示的
%带有右键菜单的GUI figure('Menubar','none'); h = uicontextmenu; uimenu(h,'Label','A'); uimenu(h,'Label','B' ...
- MySQL 5.7 时间显示修改(log_timestamps UTC)
https://blog.csdn.net/leshami/article/details/78952842 在MySQL 5.7版本中,日志记录时间发生了变化,使用了UTC方式来记录日志时间,也就是 ...
随机推荐
- 转: HTTP Live Streaming直播(iOS直播)技术分析与实现
http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html HTTP Live Streaming直播(iOS直播)技术分析与实现 ...
- HTML5 Canvas 绘制库存变化折线
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...
- 基于.NET的轻量级微信SDK
一.前言 特别不喜欢麻烦的一个人,最近碰到了微信开发.下载下来了一些其他人写的微信开发“框架”,但是被恶心到了,实现的太臃肿啦. 最不喜欢的就是把微信返回的xml消息在组装成实体类,所以会比较臃肿,现 ...
- arm-linux-gdb+gdbserver环境搭建以及远程调试
0) gdb源码下载:http://ftp.gnu.org/gnu/gdb/ 1) 编译arm-linux-gdb 指定交叉编译工具链的位置 export PATH=$PATH:/usr/local/ ...
- 利用 MySQL 技能学习 DB2 Express: DB2 与 MySQL 的管理任务和基本任务
原文地址:http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0602tham2/index.html 简单介绍 管理不 ...
- Django——WEB三层架构与MVC
而我发此文的目的有二:一者,让初学者能够听到一家之言,是为解惑:二者,更希望抛砖引玉,得到专家的批判. 许多学生经常问我,MVC到底和WEB三层架构有啥关系? 开始时,我也只能给他们一些模糊的回答.时 ...
- js比较数字相等
示例代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...
- [翻译]JUnit 5 用户手册
为了系统的学习下 JUnit 5, 因此开始翻译 JUnit 5 官方用户手册, 谢谢关注! 本手册翻译自 5.0.0-M4 版本. 若感兴趣, 可 star 或 fork 该仓库! GitHub: ...
- 在github上建立jekyll blog
1.git常用命令(简易介绍见http://rogerdudler.github.io/git-guide/index.zh.html) git init #创建新的git仓库 git clo ...
- python机器学习-逻辑回归
1.逻辑函数 假设数据集有n个独立的特征,x1到xn为样本的n个特征.常规的回归算法的目标是拟合出一个多项式函数,使得预测值与真实值的误差最小: 而我们希望这样的f(x)能够具有很好的逻辑判断性质,最 ...