public int Charcount(String string, String str) { int ii = 0; if (string == null) return 0; int[] count = new int[string.length()]; // 将字符串中索引字符存在临时变量中 char mid = str.charAt(0); for (int j = 0; j < count.length; j++) { char temp = string.charAt(j); if (mid == temp) ii++; } return ii; }
public static int compare_date(String DATE1, String DATE2) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm"); try { Date dt1 = df.parse(DATE1); Date dt2 = df.parse(DATE2); if (dt1.getTime() > dt2.getTime()) { System.out.println("dt1 在dt2后"); return 1; } else if (dt1.getTime() < dt2.getTime()) { System.out.println("dt1在dt2前"); return -1; } else { return 0; } } catch (Exception exception) { exception.printStackTrace(); } return 0; } public static void main(String args[]) { int i= compare_date("1995-11-12 15:21", "1999-12-11 09:59"); System.out.println("i=="+i); }
private final static Pattern PATTERN_TIME = Pattern .compile("\\d{4}-\\d{1,2}-\\d{1,2}"); private final static Pattern PATTERN_TIME_HALF = Pattern .compile("\\d{2}-\\d{1,2}-\\d{1,2}"); private final static Pattern PATTERN_Month = Pattern .compile("\\d{4}-\\d{1,2}"); private final static Pattern PATTERN_YEAR = Pattern.compile("\\d{4}"); private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); protected long formatTime(String value) { if (value != null && !value.equals("")) { value = value.replaceAll("\\.|年|月", "-"); Matcher matcher = PATTERN_TIME.matcher(value); Matcher matcherHalf = PATTERN_TIME_HALF.matcher(value); Matcher matcherMonth = PATTERN_Month.matcher(value); Matcher matcherYear = PATTERN_YEAR.matcher(value); if (matcher.find()) { value = matcher.group(); } else if (matcherHalf.find()) { value = "20" + matcherHalf.group(); } else if (matcherMonth.find()) { value = matcherMonth.group() + "-01"; } else if (matcherYear.find()) { value = matcherYear.group() + "-01-01"; } if (value.contains("待定") || value.contains("暂无") || value.equals("") || value.equals(" ") || value.equals("0") ) { System.out.println(value); return 0; } value = value.replaceAll(" | ", ""); value = value.replaceAll("\\.|年|月", "-"); value = value.replaceAll("日|秒", " "); value = value.replaceAll("时|分", ":"); int temn = charCount(value, ":"); if (temn == 0) { value += " 00:00:00"; } else if (temn == 1) { value += ":00"; } else if (temn == 2) { value += "00"; } try { return simpleDateFormat.parse(value).getTime(); } catch (Exception e) { e.printStackTrace(); } } return 0; }
public static void main(String[] args) { String string = "富民县J2012-122号"; string = string.replaceAll("([\u4E00-\u9FA5]+)|([\u4E00-\u9FA5]+)", ""); System.out.println("[" + string + "]"); }
Pattern pattern = Pattern.compile("[,;]+"); String[] strs = pattern.split("Java Hello World Java,Hello,,World;Sun"); for (int i=0;i<strs.length;i++) { System.out.println(strs[i]); }
protected String formatMaxNumber(String value) { List<Float> fl = new ArrayList<Float>(); if (value != null) { Matcher valueMatcher = intPattern.matcher(value); while (valueMatcher.find()) { String strRjl = valueMatcher.group(); float rst = Float.valueOf(strRjl); fl.add(rst); } if (fl.size() == 1) { return fl.get(0).toString(); } else if (fl.size() == 2) { if (fl.get(0) > fl.get(1)) { return fl.get(0).toString(); } else { return fl.get(1).toString(); } } } return null; }
private final static Pattern PATTERN_PRICE = Pattern .compile("(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))"); // 全部转换为平方米 public float formatArea(String value) { Matcher matcher = PATTERN_PRICE.matcher(value); if (matcher.find()) { if (value.contains("亩") && !value.contains("平方米")){ return String.format("%.2f",Float.parseFloat(matcher.group()) * 666.67); } else if (value.contains("公顷") && !value.contains("平方米")) { return String.format("%.2f",Float.parseFloat(matcher.group()) * 10000); } else if ((value.contains("亩") && value.contains("平方米"))|| (value.contains("公顷") && value.contains("平方米")) ) { return formatMaxNumber(value); } else { return matcher.group(); } } else { grantarea.setUseValue("0.0"); } return grantarea; } protected String formatMaxNumber(String value) { List<Float> fl = new ArrayList<Float>(); if (value != null) { Matcher valueMatcher = intPattern.matcher(value); while (valueMatcher.find()) { String strRjl = valueMatcher.group(); float rst = Float.valueOf(strRjl); fl.add(rst); } if (fl.size() == 1) { return fl.get(0).toString(); } else if (fl.size() == 2) { if (fl.get(0) > fl.get(1)) { return fl.get(0).toString(); } else { return fl.get(1).toString(); } } } return null; }
public static void main(String[] args) throws IOException { testConvertZwrq(); } private static void testConvertZwrq() { String[] list = new String[] { "二○○九年四月三十日", "○九年四月三十日", "二○○九年十二月三十一日", "二零零九年十二月三十一日" }; for (String s : list) { Date d = convertCnDate(s); System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(d)); } } public static Date convertCnDate(String cprq) { int yearPos = cprq.indexOf("年"); int monthPos = cprq.indexOf("月"); String cnYear = cprq.substring(0, yearPos); String cnMonth = cprq.substring(yearPos + 1, monthPos); String cnDay = cprq.substring(monthPos + 1, cprq.length() - 1); String year = ConvertCnYear(cnYear); String month = ConvertCnDateNumber(cnMonth); String day = ConvertCnDateNumber(cnDay); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, Integer.parseInt(year)); c.set(Calendar.MONTH, Integer.parseInt(month)-1); c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day)); return c.getTime(); } private static String ConvertCnYear(String cnYear) { if(cnYear.length() == 2) return "20" + ConvertCnNumberChar(cnYear); else return ConvertCnNumberChar(cnYear); } private static String ConvertCnDateNumber(String cnNumber) { if (cnNumber.length() == 1) { if(cnNumber.equals("十")) return "10"; else return ConvertCnNumberChar(cnNumber); } else if (cnNumber.length() == 2) { if (cnNumber.startsWith("十")) { return "1" + ConvertCnNumberChar(cnNumber.substring(1, 2)); } else if (cnNumber.endsWith("十")) { return ConvertCnNumberChar(cnNumber.substring(0, 1)) + "0"; } else { return ConvertCnNumberChar(cnNumber); } } else if (cnNumber.length() == 3) { return ConvertCnNumberChar(cnNumber.substring(0, 1) + cnNumber.substring(2, 3)); } return null; } private static String ConvertCnNumberChar(String cnNumberStr) { String ALL_CN_NUMBER = "○零一二三四五六七八九"; String ALL_NUMBER = "00123456789"; StringBuffer buf = new StringBuffer(); for (int i = 0; i < cnNumberStr.length(); i++) { char c = cnNumberStr.charAt(i); int index = ALL_CN_NUMBER.indexOf(c); if (index != -1) { buf.append(ALL_NUMBER.charAt(index)); } else { buf.append(cnNumberStr.charAt(i)); } } return buf.toString(); }
BigDecimal bg=new BigDecimal("3.456785E-8"); System.out.println(bg.toPlainString());
/** * 在数字型字符串千分位加逗号 * @param str * @return */ public static String addComma(String str){ boolean neg = false; if (str.startsWith("-")){ //处理负数 str = str.substring(1); neg = true; } String tail = null; if (str.indexOf('.') != -1){ //处理小数点 tail = str.substring(str.indexOf('.')); str = str.substring(0, str.indexOf('.')); } StringBuilder sb = new StringBuilder(str); sb.reverse(); for (int i = 3; i < sb.length(); i += 4){ sb.insert(i, ','); } sb.reverse(); if (neg){ sb.insert(0, '-'); } if (tail != null){ sb.append(tail); } return sb.toString(); }
jsp页面把数值变成千分显示可以用:
Number格式化显示
1.头部引入:<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
2.具体使用:
整数显示 <fmt:formatNumber value="${ myList.money }" pattern="#" type="number"/>
整数带千分符显示:
<fmt:formatNumber value="${num}" type="number"/>
/** * 用于在EL中获取数字整数部分 * * @param header * @param end * @return */ public static int getInt(Double num) { return num.intValue(); }
/** * 用于在EL中判断数字是否有小数位 * * @param header * @param end * @return */ public static Boolean getHaveFloatNumFlag(Double num) { return num>num.intValue(); }
/** * 用于在EL中连接字符串header+end * * @param header * @param end * @return */ public static String contact(String header, String end) { return header.concat(end); }
/** * 查找最接近目标值的数,并返回 索引 * @param array * @param mutiple * @return */ public static Integer binarysearchKey(Object[] array, float mutiple) { Arrays.sort(array); int left = 0, right = 0; for (right = array.length - 1; left != right;) { int midIndex = (right + left) / 2; int mid = (right - left); Float midValue = Float.valueOf(array[midIndex].toString()); if (mutiple == midValue) { return midIndex; } if (mutiple > midValue) { left = midIndex; } else { right = midIndex; } if (mid <= 2) { break; } } System.out.println("要查找的数:" + mutiple + "最接近的数:" +(Math.abs(Float.valueOf(array[right].toString()) -mutiple) > Math.abs(Float.valueOf(array[left].toString()) -mutiple) ? array[left] : array[right])); return (Integer) (Math.abs(Float.valueOf(array[right].toString()) -mutiple) > Math.abs(Float.valueOf(array[left].toString()) -mutiple) ? left: right); }