1.以友好的方式显示时间
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.format(cal.getTime());
String paramDate = dateFormater2.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.format(time);
}
return ftime;
}
2.判断给定字符串是否空白串(空白串是指由空格、制表符、回车符、换行符组成的字符串)
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;
}
3.判断给定的日期是不是今天
private final static SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private final static SimpleDateFormat dateFormater2 = new SimpleDateFormat("yyyy-MM-dd");
/**
* 将字符串转位日期类型
* @param sdate
* @return
*/
public static Date toDate(String sdate) {
try {
return dateFormater.parse(sdate);
} catch (ParseException e) {
return null;
}
}
/**
* 判断给定字符串时间是否为今日
* @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.format(today);
String timeDate = dateFormater2.format(time);
if(nowDate.equals(timeDate)){
b = true;
}
}
return b;
}
4.判断字符串是否符合邮箱格式
public static boolean isEmail(String email){
if(email == null || email.trim().length()==0)
return false;
return emailer.matcher(email).matches();
}