package com.partynetwork.dataprovider.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.util.LogUtils;
import com.partynetwork.iparty.R;
import com.partynetwork.iparty.app.AppConfig;
import com.partynetwork.iparty.app.AppContext;
import com.partynetwork.iparty.app.AppException;
/**
* UI的通用工具类
*/
public class StringUtil {
public static final int Information = 0;
/**
* 正则表达式验证 判断邮箱是否正确
*
* @param text
* 输入的邮箱
* @return 是否为邮箱
*/
public static boolean filterEmail(String text) {
/*
* 邮箱的正则表达式
*/
String expression = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/* 创建Pattern */
Pattern pattern = Pattern.compile(expression);
/* 将Pattern 以参数传入Matcher作Regular expression */
Matcher matcher = pattern.matcher(text);
return matcher.matches();
}
/**
* 正则表达式验证 判断手机号码是否正确
*
* @param text
* 输入的手机号
* @return 是否为手机号码
*/
public static boolean filterPhoneNumber(String text) {
/*
* "-" 继续. (\\d{4})$: 以四个数字结束. 可以比较下列数字格式:
* 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
* 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通)
*/
String expression = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
/* 创建Pattern */
Pattern pattern = Pattern.compile(expression);
/* 将Pattern 以参数传入Matcher作Regular expression */
Matcher matcher = pattern.matcher(text);
return matcher.matches();
}
/**
* 正则表达式验证 判断密码是否正确
*
* @param text
* 输入的密码
* @return 是否为正确的密码
*/
public static boolean filterPassword(String text) {
/*
* 密码正则表达式正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。
*/
String expression = "^\\w{6,17}$";
/* 创建Pattern */
Pattern pattern = Pattern.compile(expression);
/* 将Pattern 以参数传入Matcher作Regular expression */
Matcher matcher = pattern.matcher(text);
return matcher.matches();
}
/**
* 将字符串转为时间戳
*
* @param time
* @return
*/
public static String getTime(String time) {
String re_time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分",
Locale.CHINA);
Date d;
try {
d = sdf.parse(time);
long l = d.getTime();
String str = String.valueOf(l);
re_time = str.substring(0, 10);
} catch (ParseException e) {
e.printStackTrace();
}
return re_time;
}
/**
* 将字符串转为Calendar对象
*
* @param time
* @return
*/
public static Calendar getTime2Calendar(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分",
Locale.CHINA);
Date d;
Calendar cal = Calendar.getInstance();
try {
d = sdf.parse(getStrTime(time));
cal.setTime(d);
} catch (ParseException e) {
e.printStackTrace();
}
return cal;
}
/**
* 将时间戳转为字符串
*
* @param cc_time
* @return
*/
public static String getStrTime(String cc_time) {
if (checkStr(cc_time).equals("")) {
return "";
}
String re_StrTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分",
Locale.CHINA);
long lcc_time = Long.valueOf(checkStr(cc_time));
// lcc_time += 28800;
re_StrTime = sdf.format(new Date(lcc_time * 1000L));
return re_StrTime;
}
/**
* 获取两个时间戳时间的所有日期
*
* @param p_start
* 开始时间戳
* @param p_end
* 结束时间戳
* @return Dates List
*/
public static List<String> getDates(String p_start, String p_end, int[] days) {
List<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分",
Locale.CHINA);
Calendar temp = (Calendar) getTime2Calendar(p_start).clone();
if (temp.equals(getTime2Calendar(p_end))) {
// 如果两个时间为同一天
result.add(sdf.format(temp.getTime()));
} else {
while (temp.before(getTime2Calendar(p_end))) {
if (days != null && days.length > 0) {
for (int day : days) {
if (day == getWeekDay(sdf.format(temp.getTime()))) {
result.add(sdf.format(temp.getTime()));
}
}
} else {
result.add(sdf.format(temp.getTime()));
}
temp.add(Calendar.DAY_OF_YEAR, 1);
}
}
return result;
}
/**
* 取指定时间为星期几
*
* @param DateStr
* @return
*/
public static int getWeekDay(String DateStr) {
SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy年MM月dd日HH时mm分",
Locale.CHINA);
SimpleDateFormat formatD = new SimpleDateFormat("E", Locale.CHINA);// "E"表示"day in week"
Date d = null;
String weekDay = "";
try {
d = formatYMD.parse(DateStr);// 将String 转换为符合格式的日期
weekDay = formatD.format(d);
} catch (Exception e) {
e.printStackTrace();
}
int day = 0;
if (weekDay.equals("周一")) {
day = 1;
} else if (weekDay.equals("周二")) {
day = 2;
} else if (weekDay.equals("周三")) {
day = 3;
} else if (weekDay.equals("周四")) {
day = 4;
} else if (weekDay.equals("周五")) {
day = 5;
} else if (weekDay.equals("周六")) {
day = 6;
} else if (weekDay.equals("周日")) {
day = 0;
} else {
day = 0;
}
LogUtils.i(DateStr + "是" + weekDay);
return day;
}
/**
* 获得传入时间和当前时间差!返回String
*
* @param before
* @return
*/
public static String getTimeBefore(long before) {
int temp = (int) ((System.currentTimeMillis() - before) / 1000) / 60;
if (temp == 0) {
return "一分钟内";
} else if (temp < 60) {
return temp + "分钟前";
} else if (temp < 60 * 24 * 4) {
int hours = temp / 60;
if (hours < 24) {
return hours + "小时前";
} else if (hours < 48) {
return "一天前";
} else if (hours < 72) {
return "两天前";
}
return "三天前";
} else {
Date date = new Date(before);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
return simpleDateFormat.format(date);
}
}
/**
* 判断edittext是否null
*/
public static String checkEditText(EditText editText) {
if (editText != null && editText.getText() != null
&& !(editText.getText().toString().trim().equals(""))) {
return editText.getText().toString().trim();
} else {
return "";
}
}
/**
* 判断textview是否null
*/
public static String checkTextView(TextView textView) {
if (textView != null && !textView.getText().toString().equals("")) {
return textView.getText().toString();
} else {
return "";
}
}
/**
* 检查字符串
*
* @param str
* @return
*/
public static String checkStr(String str) {
if (str != null && !str.equals("")) {
return str;
} else {
return "";
}
}
/**
* 把字符串放到TextView中
*
* @param tv
* @param str
*/
public static void setText(TextView tv, String str) {
if (tv == null) {
return;
}
tv.setText(checkStr(str));
}
/**
* 把字符串放到TextView中
*
* @param tv
* @param str
*/
public static void setText(TextView tv, int str) {
if (tv == null) {
return;
}
tv.setText(str + "");
}
/**
* 把url加载到图片中
*
* @param imageView
* @param url
*/
// public static void setImage(ImageView imageView, String url) {
// BitmapUtils mHeadBitmapUtils = new BitmapUtils(imageView.getContext());
// mHeadBitmapUtils.display(imageView, url);
// }
/**
* 加载头像
*
* @param imageView
* @param url
*/
// public static void setHeadImage(ImageView imageView, String url) {
// BitmapUtils mHeadBitmapUtils = new BitmapUtils(imageView.getContext());
// mHeadBitmapUtils.configDefaultBitmapMaxSize(100, 100);
// mHeadBitmapUtils.configDefaultLoadingImage(R.drawable.default_head_boy);
// mHeadBitmapUtils
// .configDefaultLoadFailedImage(R.drawable.default_head_neutral);
// mHeadBitmapUtils.display(imageView, url);
// }
/**
* 格式化float取两位小数
*
* @param money
* @return
*/
public static String formatFloat(float money) {
return new DecimalFormat("0.00").format(money);
}
/**
* 截取指定分隔符前面的字符串
*
* @param s
* @param separator
* @return
*/
public static String getLeftString(String s, String separator) {
int index = s.indexOf(separator);
if (index > -1)
return s.substring(0, index);
else
return s;
}
public static String convertStreamToString(InputStream is)
throws AppException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
throw AppException.io(e);
} finally {
try {
is.close();
} catch (IOException e) {
throw AppException.io(e);
}
}
return sb.toString();
}
/**
* 过滤Html代码
*
* @param inputString
* @return
*/
public static String Html2Text(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
java.util.regex.Pattern p_html1;
java.util.regex.Matcher m_html1;
try {
String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[//s//S]*?<///script>
String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[//s//S]*?<///style>
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
String regEx_html1 = "<[^>]+";
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
p_html1 = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE);
m_html1 = p_html1.matcher(htmlStr);
htmlStr = m_html1.replaceAll(""); // 过滤html标签
textStr = htmlStr;
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}
return textStr;// 返回文本字符串
}
/**
* 另外一种方法解析表情
*
* @param message
* 传入的需要处理的String
* @return
*/
public static CharSequence convertNormalStringToSpannableString(
Context context, String message) {
// TODO Auto-generated method stub
String hackTxt;
if (message == null || message.equals("")) {
return "";
}
if (message.startsWith("[") && message.endsWith("]")) {
hackTxt = message + "";
} else {
hackTxt = message;
}
SpannableString value = SpannableString.valueOf(hackTxt);
Matcher localMatcher = Pattern.compile("\\[(\\S+?)\\]").matcher(value);
while (localMatcher.find()) {
String str2 = localMatcher.group(0);
int k = localMatcher.start();
int m = localMatcher.end();
// k = str2.lastIndexOf("[");
// Log.i("way", "str2.length = "+str2.length()+", k = " + k);
// str2 = str2.substring(k, m);
if (m - k < 8) {
AppConfig config = AppConfig.getAppConfig(AppContext
.getInstance());
if (config.getFaceMap().containsKey(str2)) {
int face = config.getFaceMap().get(str2);
Bitmap bitmap = BitmapFactory.decodeResource(
context.getResources(), face);
if (bitmap != null) {
ImageSpan localImageSpan = new ImageSpan(context,
bitmap, ImageSpan.ALIGN_BASELINE);
value.setSpan(localImageSpan, k, m,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
return value;
}
public static boolean isEmpty(String savePath) {
if (savePath == null || savePath.equals("")) {
return true;
}
return false;
}
}