CommonUtil类

  • Post author:
  • Post category:其他


金融系统中常用的一些工具类

     /**
     * 将贷记有数字标识变成中文
     * @param dcflag
     * @return
     */
    public static String dcflag2CN(String dcflag){
        if(dcflag != null&&dcflag.length()==1)
            return dcflag.equals("1")?"借":(dcflag.equals("2")?"贷":(dcflag.equals("3")?"指示业务":dcflag));
        else
            return dcflag;
    }
	/**获取客户端真实IP地址*/
	public static String getIpAddr(HttpServletRequest request) {
	       String ip = request.getHeader("x-forwarded-for");
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getHeader("Proxy-Client-IP");
	       }
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getHeader("WL-Proxy-Client-IP");
	       }
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getRemoteAddr();
	       }
	       return ip;
	   }
     /**
     * 将金额转换成以XXX标准格式
     * pattern是需要的格式,默认为保留两位小数
     * @param inputStr
     * @return
     */
    public static String moneyFormat(double d,String pattern){
        pattern=(pattern==null||pattern.equals(""))?"#,##0.00":pattern;
        DecimalFormat df = new DecimalFormat(pattern);
        try{
            return df.format(d);
        }catch(Exception e){
            return String.valueOf(d);
        }
    }
    /**
     * 将整数count转化成num位的字符串位数不够就补零
     * @param count
     * @param num
     * @return
     */
    public static String countFormat(int count,int num){
        String tmp = String.valueOf(count);
        int len = String.valueOf(count).length();
        if(len<num){
            for(int i =0;i<num-len;i++){
                tmp = "0"+tmp;
            }
        }
        return tmp;
    }
    /**
     * 传入一个小数输出一个相应的百分比格式,传入的那个数要求很高
     * @param src
     * @return
     */
    public static String getPercent(double src) {
        String dest = null;
        try {
            NumberFormat formater = NumberFormat.getPercentInstance();
            formater.setGroupingUsed(false);
            formater.setMaximumFractionDigits(2);
            dest = formater.format(src);
        } catch (Exception e) {
            dest = null;
        }
        return dest;
    }
	/**
	 * 将日期中的斜线去掉
	 */
	public static String cutDateStr(String date){
		if(date != null &&date.length()==10)
			return date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10);
		else
			return date;
	}
	/**
	 * 将日期中间加上斜线
	 */
	public static String addDateStr(String date){
		if(date != null &&date.length()==8)
			return date.substring(0, 4) + "/" + date.substring(4, 6) + "/" + date.substring(6, 8);
		else
			return date;
	}
    /**
     * 格式化日期,日期的格式为6位的年月日
     * @param time
     * @return
     */
    public static String addTimeStr(String time){
        if(time != null && time.length()==6)
            return time.substring(0, 2) + ":" + time.substring(2, 4) + ":" + time.substring(4, 6);
        else
            return time;
    }
    /**
     * 格式化日期时间格式为:年月日时分秒
     * @param dateTime
     * @return
     */
    public static String formatDateTime(String dateTime){
        if(dateTime != null &&dateTime.length()==14)
            return dateTime.substring(0, 4) + "/" + dateTime.substring(4, 6) + "/" + dateTime.substring(6, 8)+" " +
                    dateTime.substring(8, 10) + ":" + dateTime.substring(10, 12) + ":" + dateTime.substring(12, 14);
        else
            return dateTime;
    }
     /**
     * 取得十四位的日期时间
     * @return
     */
    public static String getYMDHms(){
        Date d = Calendar.getInstance().getTime();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sf.format(d);
    }
    /**
     * 将日期转换成带中文的日期格式
     */
    public static String date2CN(String date){
        if(date != null &&date.length()==8)
            return date.substring(0, 4) + "年" + date.substring(4, 6) + "月" + date.substring(6, 8)+"日";
        else if(date!=null && date.length()==10)
            return date.substring(0, 4) + "年" + date.substring(5, 7) + "月" + date.substring(8, 10)+"日";
        else if(date!=null && date.length()==7)
            return date.substring(0, 4) + "年" + date.substring(5, 7) + "月" ;
        else
            return date;
    }
	/**
	 * 生成一个随机整数
	 * @return
	 */
	public static int getOneRandom(){
		Random random = new Random(System.currentTimeMillis());
		return random.nextInt(9999);
	}
        /**
	 * 格式化日期
	 * @param date
	 * @return
	 */
	public static String getDate(Date date, String pattern){
		SimpleDateFormat sdf=new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
  /**
     * 格式化日期
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getDate(String date, String pattern) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }
  /**
     * 计算两个日期间的月份数(包括首尾月)
     * @param startCal
     * @param endCal
     * @return
     */
    public static int getMonthOfDate(Calendar startCal,  Calendar endCal){
        int startYear = startCal.get(Calendar.YEAR);
        int startMonth = startCal.get(Calendar.MONTH);
        int endYear = endCal.get(Calendar.YEAR);
        int endMonth = endCal.get(Calendar.MONTH);
        int month = (endYear*12+endMonth) - (startYear*12+startMonth) + 1;
        return month;
    }

/**
     * 传入一个路径,返回文件名
     * @param path
     * @return
     */
   public static String filenameOfPath(String path){
       String temp[] = path.split("\\\\");
       String fileName = temp[temp.length-1];
       return fileName;
   }



版权声明:本文为lingmao555原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。