zoukankan      html  css  js  c++  java
  • Lifyray笑傲江湖之API总结TextUtil

    package liferay;
    
    /**
    *
    */
    
    
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.util.Date;
    import java.util.Random;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    
    /**
    * @author Ivan
    * 
    */
    public class TextUtil {
    
        private static Pattern N = Pattern.compile("
    ");
        private static Pattern R = Pattern.compile("
    ");
        private static Pattern RN = Pattern.compile("
    ");
        private static Pattern NR = Pattern.compile("
    
    ");
        private final static String BR = "<br/>";
    
        private static NumberFormat decimalFormat = DecimalFormat
                .getNumberInstance();
    
        public static String doubleToText(double number) {
            String txt = doubleToText(number, 1);
            return txt;
        }
    
        public static String doubleToText(double number, int digit) {
            String txt = "";
            if (number > 0) {
                decimalFormat.setMaximumFractionDigits(digit);
                txt = decimalFormat.format(number);
            }
            return txt;
        }
    
        public static String doubleToText2(double number, int digit) {
            String txt = "";
            if (number > 0) {
                DecimalFormat decimalFormat2 = new DecimalFormat();
                decimalFormat2.setGroupingSize(0);
                decimalFormat2.setMaximumFractionDigits(digit);
                txt = decimalFormat2.format(number);
            }
            return txt;
        }
    
        public static String doubleToText(double number, String format) {
            if (format.equals("")) {
                format = "#,##0.00";
            }
    
            String txt = "";
            if (number > 0) {
                DecimalFormat decimalFormat = new DecimalFormat(format);
                txt = decimalFormat.format(number);
            }
            return txt;
        }
    
        public static String textToHtml(String txt) {
            Matcher m = RN.matcher(txt);
            txt = m.replaceAll(BR);
            m = NR.matcher(txt);
            txt = m.replaceAll(BR);
            m = N.matcher(txt);
            txt = m.replaceAll(BR);
            m = R.matcher(txt);
            txt = m.replaceAll(BR);
            return txt;
        }
    
        public static void main(String[] args) {
    //        long longDuration = new Date().getTime() - 46450000;
    //        longDuration = new Date().getTime() - longDuration;
    //        double doubleDuration = longDuration;
    //        doubleDuration = doubleDuration / 86400000;
    //        System.out.println(doubleDuration);
    //        System.out.println(doubleToText(doubleDuration));
            
            String sql = getRandomString(5);
            System.out.println(sql);
            
            
            
            
            
            
        }
    
        public static String doSubAssignLength(String str, int length) {
            String commtents = null;
            if (str.length() > length) {
                commtents = str.substring(0, length - 1) + "...";
            } else {
                commtents = str;
            }
            return commtents;
        }
    
        public static String doSubString(String str) {
            String commtents = null;
            if (str.length() > 26) {
                commtents = str.substring(0, 25) + "...";
            } else {
                commtents = str;
            }
            return commtents;
        }
    
        
    
        public static String getURL(String layoutId, String url) {
            StringBuffer buffer = new StringBuffer();
            buffer.append(url);
            if (layoutId != null && !"0".equals(layoutId)) {
                int start = url.lastIndexOf("/");
                int end = url.indexOf("?");
                buffer.replace(start, end, layoutId);
            }
            return buffer.toString();
        }
    
        public static String toUpperCase(String tableName) {
            String prefix = tableName.substring(0, 1).toUpperCase();
            String results = prefix + tableName.substring(1);
            return results;
        }
    
        public static String getRandomString(int length) {
            char[] charArray = new char[length];
            for (int i = 0; i < length; i++) {
                Random r = new Random();
                int n = r.nextInt(123);
                while (n < 48 || (n > 57 && n < 65) || (n > 90 && n < 97)
                        || n > 122) {// (!((n>=48 && n<=57) || (n>=65 && n<=90)
                                        // && (n>=97 && n<=122))){
                    n = r.nextInt(123);
                }
                charArray[i] = (char) n;
            }
            return String.valueOf(charArray);
        }
    
        public static String convertFileSize(long filesize) {
            String strUnit = "Bytes";
            String strAfterComma = "";
            int intDivisor = 1;
            if (filesize >= 1024 * 1024) {
                strUnit = " MB";
                intDivisor = 1024 * 1024;
            } else if (filesize >= 1024) {
                strUnit = " KB";
                intDivisor = 1024;
            }
            if (intDivisor == 1)
                return filesize + " " + strUnit;
    
            strAfterComma = "" + 100 * (filesize % intDivisor) / intDivisor;
            if (strAfterComma == "")
                strAfterComma = ".0";
    
            return filesize / intDivisor + "." + strAfterComma + " " + strUnit;
    
        }
        
    }
  • 相关阅读:
    用grunt搭建自动化的web前端开发环境-完整教程
    redis 使用
    ubuntu系统安装redis
    redis resque消息队列
    linux下常用的命令
    介绍Mina的TCP的主要接口(一)
    Mina框架详解——简单的TCPClient
    Mina框架详解——简单的TCPServer
    Mina框架知识解析
    RabbitMQ新手入门Helloworld
  • 原文地址:https://www.cnblogs.com/airycode/p/4812698.html
Copyright © 2011-2022 走看看