package net.oschina.app.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.TimeZone; 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 IMG_URL = Pattern .compile(".*?(gif|jpeg|png|jpg|bmp)"); private final static Pattern URL = Pattern .compile("^(https|http)://.*?$(net|com|.com.cn|org|me|)"); 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) { return toDate(sdate, dateFormater.get()); } public static Date toDate(String sdate, SimpleDateFormat dateFormater) { try { return dateFormater.parse(sdate); } catch (ParseException e) { return null; } } public static String getDateString(Date date) { return dateFormater.get().format(date); } /** * 以友好的方式显示时间 * * @param sdate * @return */ public static String friendly_time(String sdate) { Date time = null; if (TimeZoneUtil.isInEasternEightZones()) time = toDate(sdate); else time = TimeZoneUtil.transformTime(toDate(sdate), TimeZone.getTimeZone("GMT+08"), TimeZone.getDefault()); 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 < 31) { ftime = days + "天前"; } else if (days >= 31 && days <= 2 * 31) { ftime = "一个月前"; } else if (days > 2 * 31 && days <= 3 * 31) { ftime = "2个月前"; } else if (days > 3 * 31 && days <= 4 * 31) { ftime = "3个月前"; } else { ftime = dateFormater2.get().format(time); } return ftime; } public static String friendly_time2(String sdate) { String res = ""; if (isEmpty(sdate)) return ""; String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; String currentData = StringUtils.getDataTime("MM-dd"); int currentDay = toInt(currentData.substring(3)); int currentMoth = toInt(currentData.substring(0, 2)); int sMoth = toInt(sdate.substring(5, 7)); int sDay = toInt(sdate.substring(8, 10)); int sYear = toInt(sdate.substring(0, 4)); Date dt = new Date(sYear, sMoth - 1, sDay - 1); if (sDay == currentDay && sMoth == currentMoth) { res = "今天 / " + weekDays[getWeekOfDate(new Date())]; } else if (sDay == currentDay + 1 && sMoth == currentMoth) { res = "昨天 / " + weekDays[(getWeekOfDate(new Date()) + 6) % 7]; } else { if (sMoth < 10) { res = "0"; } res += sMoth + "/"; if (sDay < 10) { res += "0"; } res += sDay + " / " + weekDays[getWeekOfDate(dt)]; } return res; } /** * 获取当前日期是星期几<br> * * @param dt * @return 当前日期是星期几 */ public static int getWeekOfDate(Date dt) { Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return w; } /** * 判断给定字符串时间是否为今日 * * @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); } public static String getCurTimeStr() { Calendar cal = Calendar.getInstance(); String curDate = dateFormater.get().format(cal.getTime()); return curDate; } /*** * 计算两个时间差,返回的是的秒s * * @author 火蚁 2015-2-9 下午4:50:06 * * @return long * @param dete1 * @param date2 * @return */ public static long calDateDifferent(String dete1, String date2) { long diff = 0; Date d1 = null; Date d2 = null; try { d1 = dateFormater.get().parse(dete1); d2 = dateFormater.get().parse(date2); // 毫秒ms diff = d2.getTime() - d1.getTime(); } catch (Exception e) { e.printStackTrace(); } return diff / 1000; } /** * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为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(); } /** * 判断一个url是否为图片url * * @param url * @return */ public static boolean isImgUrl(String url) { if (url == null || url.trim().length() == 0) return false; return IMG_URL.matcher(url).matches(); } /** * 判断是否为一个合法的url地址 * * @param str * @return */ public static boolean isUrl(String str) { if (str == null || str.trim().length() == 0) return false; return URL.matcher(str).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; } public static String getString(String s) { return s == null ? "" : s; } /** * 将一个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 + "<br>"); 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(); } /*** * 截取字符串 * * @param start * 从那里开始,0算起 * @param num * 截取多少个 * @param str * 截取的字符串 * @return */ public static String getSubString(int start, int num, String str) { if (str == null) { return ""; } int leng = str.length(); if (start < 0) { start = 0; } if (start > leng) { start = leng; } if (num < 0) { num = 1; } int end = start + num; if (end > leng) { end = leng; } return str.substring(start, end); } /** * 获取当前时间为每年第几周 * * @return */ public static int getWeekOfYear() { return getWeekOfYear(new Date()); } /** * 获取当前时间为每年第几周 * * @param date * @return */ public static int getWeekOfYear(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); int week = c.get(Calendar.WEEK_OF_YEAR) - 1; week = week == 0 ? 52 : week; return week > 0 ? week : 1; } public static int[] getCurrentDate() { int[] dateBundle = new int[3]; String[] temp = getDataTime("yyyy-MM-dd").split("-"); for (int i = 0; i < 3; i++) { try { dateBundle[i] = Integer.parseInt(temp[i]); } catch (Exception e) { dateBundle[i] = 0; } } return dateBundle; } /** * 返回当前系统时间 */ public static String getDataTime(String format) { SimpleDateFormat df = new SimpleDateFormat(format); return df.format(new Date()); } }
相关推荐
String 字符串操作工具类,sql防注入方法 过滤通过页面表单提交的字符 用特殊的字符连接字符串 将字符串数组转换为逗号链接的字符串,并且去掉最后一个逗号 分割字符串 字符串字符集转换 编码转换方法 将html文档...
它是一组有用的字符串操作函数,提供了用于连接、拆分、翻译字符串的多功能功能。 MATLAB 附带了一系列字符串函数。 但是,某些有用的函数尚未包含在 MATLAB 版本中。 受 Java、Ruby 和 Python 等其他高级语言的...
本程序主要功能是:找出指定文件夹中所有包含指定制字符串的文件。 编程语言:java 操作系统:跨平台(win7,Linix已实验)
今天小编就为大家分享一篇关于java字符串中${}或者{}等的占位符替换工具类,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Ring0内核层下字符串基础操作之Ansi与Unicode字符串类型互转
Ring0内核层下字符串基础操作之任意进制的整型数据和字符串互转
1.4 字符串操作 18 1.4.1 连接字符串 18 1.4.2 从字符串中提取子串 20 1.4.3 比较字符串 20 1.4.4 字符串转换 21 1.4.5 格式化字符串 21 1.5 字符串用法 22 1.5.1 构建字符串 22 1.5.2 分析字符串 24 1.6 国际化 25 ...
时间戳(Long)转字符串,字符串转时间戳工具包 ,操作方便。
二进制字符串工具包 概括 二进制字符串工具包(Binary String Toolkit)或简称BST是一个相当简单的实用程序,用于将二进制字符串转换为适合包含在源代码中的各种格式,例如用于开发安全领域的漏洞利用和概念证明的...
工具类包 Assert提供对数组、集合、字符串的合法校验静态方法 BeanHelper提供了bean类与map的互相转换 PropertiesUtil实现对properties文件的读与写,参数文件名不包括.properties SysConfig实现对指定系统资源文件...
可以给出各阶段操作提示的汇编程序,实现了字符串的输入和显示以及退格、回车的处理,能够将输入的字符串保存到任意指定的路径下。文件夹内包含有编译连接工具,源代码中有详细注释,希望可以给汇编初学者带来一些...
通用的Java工具类,包含文件(word,exel),图片,文件加密,二维码,字符串,文件操作等工具类集合
字符串工具包安装npm i string-toolkit用法// Creating a new instance.const stringTools = new ( require ( 'string-toolkit' ) ) ( ) ;// Can also be used without a new instance.const stringTools = require ...
前端开源库-string-kit字符串工具包,一个字符串操作工具箱,具有一个字符串格式化程序(受sprintf启发)、一个变量检查器(输出具有ansi颜色和html)和各种转义函数(shell参数、regexp、html等)。
8.4 实验3 字符串的比较和查找最长子串 8.5 实验4 单词逆向输出问题 8.6 实验5 ISBN书号校验问题 第9章 Applet小应用程序 9.1 预备知识 9.2 实验1 Applet生命周期 9.3 实验2 Applet的参数提取 9.4 实验3 鼠标...
这些工具和类库使得在处理中文字符串时更加简单,包括字符类型之间的转换、操作和统计等功能,有助于简化中文软件的设计和输出过程。 其中包含字模库文件路径、字模宽度、字模高度、每个字模字节数和字模库文件缓存...
里面提供了大量,实用工具, # ImageUtil ==> 提供对图像操作工具 # TextUtil ==> 提供对字符串操作工具 # MD5Util ==> 提供对MD5算法在JAVA ME的实现类,并提供一个静态方法创建toMD5 得到一个MD5的字符串...
在自由许可下,一些简洁的函数可以读取表示布尔表达式的字符串并返回各种方便的形式。 它使用进行繁重的解析,并使用George Prekas根据Robert Dick和Pat Maupin的代码(全部包含在qm.py )编写的Quine-McCluskey...
字符串解析器这是在使用 (或 strings-alternative)命令的初步工作。入门该存储库包括 StringsParser 的实现,这是一个使用 strings 命令从二进制文件中提取 ASCII 字符串的解析器。 使用 build.sh 和 run.sh 脚本...
但是,在拼接Json格式的字符串时,如果字符串的内容过多,就容易拼出错误的字符串,而实践证明了这个字符串拼接的调试过 程明显拖延了开发的速度,因此,我们需要一个能够轻松组建Json格式字符串的工具包,来简化这...