import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.text.DecimalFormat; import org.apache.commons.lang.StringUtils; public class StrTool { /** * 日期格式化 * * @param date * Date * @param formatstr * String * @return String */ public static String formatDate(java.util.Date date, String formatstr) { if (date == null || formatstr == null) return null; java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr); return dateFormat.format(date); } public static String formatDate(java.util.Date date) { return formatDate(date, "yyyy-MM-dd"); } /** * 格式当前日期 * * @return String */ public static String formatDateByNow() { return formatDate(new java.util.Date()); } public static String formatTime(java.util.Date date) { return formatDate(date, "kk:mm:ss"); } public static String formatDateTime(java.util.Date date) { return formatDate(date, "yyyy-MM-dd kk:mm"); } /** * 解释日期yyyy-MM-dd kk:mm * * @param strdate * String * @return Date * @throws ParseException */ public static java.util.Date parseDateTime(String strdate) throws ParseException { return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd kk:mm"); } /** * 解释日期yyyy-MM-dd * * @param strdate * String * @return Date * @throws ParseException */ public static java.util.Date parseDate(String strdate) throws ParseException { return parseDateTimeByFormatstr(strdate, "yyyy-MM-dd"); } /** * 根据传入的格式化字串来解释日期 * * @param strdate * String * @param formatstr * String * @return Date * @throws ParseException */ public static java.util.Date parseDateTimeByFormatstr(String strdate, String formatstr) throws ParseException { java.text.SimpleDateFormat dateFormat = new SimpleDateFormat(formatstr); return dateFormat.parse(strdate); } /** * 格式数字类型 * * @param f * float * @param sacl * int * @return String */ public static String formatNumber(double f, String pattern) { java.text.DecimalFormat num = new DecimalFormat(pattern); return num.format(f); } /** * 格式化为钱格式显示如:1,234,555.00 * * @param money * double * @return String */ public static String formatMoney(double money) { return formatNumber(money, "#,##0.00"); } public static String indexUpStr(String str) { if (str == null || str.length() < 1) { return str; } String tmstr = ""; tmstr = str.substring(0, 1); tmstr = tmstr.toUpperCase(); tmstr += str.substring(1); return tmstr; } /** * 根据分隔符,将数字字符转换成整型数组 * * @param str * String * @param regex * String * @return int[] */ public static int[] toIntsByRegex(String str, String regex) { if (str == null || "".equals(str.trim())) return null; String[] tms = str.split(regex); int[] tmi = new int[tms.length]; for (int i = 0; i < tms.length; i++) { tmi[i] = Integer.parseInt(tms[i]); } return tmi; } public static int[] toInts(String str) { return toIntsByRegex(str, ","); } /** * 将格式化的数字还原,如:1,234,567.00——>1234567.00 * * @param formattedNumber * @return parseString */ public static String parseFormattedNumber(String formattedNumber) { java.text.DecimalFormat num = new DecimalFormat(); String parseString = ""; try { parseString = formatNumber(Double.parseDouble(num.parse( formattedNumber).toString()), "#0.00"); } catch (ParseException e) { e.printStackTrace(); } return parseString; } public static void main(String[] args) throws ParseException { // System.out.println(indexUpStr("")+";"); // toIntsByRegex(" ",","); // Date date = parseDateTime("2004-04-12 13:24"); // System.out.println(formatDateTime(date)); java.text.DecimalFormat num = new DecimalFormat(); System.out.println(formatNumber(5234556, "#,##0.00%")); System.out.println(parseFormattedNumber("99999999")); System.out.println("interval: " + getIntervalOfDate("2005-05-01", "2006-06-12")); } /** * 将字符串数组转化为字符串,并用","连接 * * @author 李奉学 * @param arrayString * @return 带","分割的字符串 */ public static String convertArrayToString(String[] arrayString) { String returnString = ""; if (arrayString != null) { for (int i = 0; i < arrayString.length; i++) { // 第一个元素前不能加"," if (StringUtils.isEmpty(returnString)) { returnString = returnString + arrayString[i]; } else { returnString = returnString + "," + arrayString[i]; } } } return returnString; } public static String getFileExt(String filename) { String fileext = ""; String tmfilename = filename; if (tmfilename != null) { int l = tmfilename.indexOf(".") + 1; if (l == 0) { return ""; } int tml = l; while (tml != 0) { try { l = tml; tml = tmfilename.indexOf(".", tml) + 1; } catch (Exception e) { tml = 0; } } int length = tmfilename.length(); if (l <= length && l != 1) { fileext = tmfilename.substring(l); } else { fileext = ""; } } return fileext; } /** * 补齐字符长度 * * @param str * @param length * @param repair * @return */ public static String fillStrLength(String str, int length, String repair) { for (int i = str.length(); i < length; i++) { str = repair + str; } return str; } /** * 比较字符串大小 * * @param str1 * @param str2 * @return */ public static int strComp(String str1, String str2) { if (str1 == null || str2 == null) { return 0; } int str1Length = str1.length(); int str2Length = str2.length(); if (str2Length > str1Length) { str1 = fillStrLength(str1, str2Length, "0"); } else if (str2Length < str1Length) { str1 = fillStrLength(str2, str1Length, "0"); } int result = str1.compareTo(str2); if (result >= 1) result = 1; if (result <= -1) result = -1; return result; } /** * xuxu 返回毫秒 * * @param date * @return */ public static long getMillis(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.getTimeInMillis(); } /** * 日期相减 * * @param date * 日期 * @param day * 天数 * @return */ public static Date subDate(Date date, int day) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000); return c.getTime(); } /** * 返回同一年内两个日期相差的天数 * * @author 李奉学 * @param date1 * @param date2 * @return 相差的天数 * @throws ParseException */ public static int getIntervalOfDate(String date1, String date2) throws ParseException { int interval = 0; // 设置日期 Calendar cal1 = Calendar.getInstance(); cal1.setTime(parseDate(date1)); Calendar cal2 = Calendar.getInstance(); cal2.setTime(parseDate(date2)); // 相差的天数 interval = Math.abs(cal1.get(Calendar.DAY_OF_YEAR) - cal2.get(Calendar.DAY_OF_YEAR)); return interval; } /** * 当前日期在一年中的周序号 * * @param date * @return 周序号 * @throws ParseException */ public static int getWeekIndexOfYear(String date) throws ParseException { int index = -1; Calendar cal = Calendar.getInstance(); cal.setTime(parseDate(date)); index=cal.get(Calendar.WEEK_OF_YEAR); return index; } }