记录一下自己涉及到的时间及金额方法的处理

/**
 * 金额format工具
 * @param xyje
 * @return 千分位协议金额
 */
public static String StringFmtMicrometer(String xyje){
    
    
    DecimalFormat df = null;
    if(xyje.indexOf(".") > 0)
    {
    
    
        if(xyje.length() - xyje.indexOf(".")-1 == 0)
        {
    
    
            df = new DecimalFormat("###,##0.");
        }else if(xyje.length() - xyje.indexOf(".")-1 == 1)
        {
    
    
            df = new DecimalFormat("###,##0.0");
        }else
        {
    
    
            df = new DecimalFormat("###,##0.00");
        }
    }else
    {
    
    
        df = new DecimalFormat("###,##0");
    }
    double number = 0.0;
    try {
    
    
        number = Double.parseDouble(xyje);
    } catch (Exception e) {
    
    
        number = 0.0;
    }
    return df.format(number);
}
/**
     * @param str 字符串
     * @return num 字符串中抽取出来的数字
     * @description 从字符串中抽取数字
     */
    public String strExtractLong(String str){
    
    
        String num="";
        if(StringUtils.isNotEmpty(str)){
    
    
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(str);
            num= m.replaceAll("").trim();
        }
        return num;
    }
  /**
     * @param str 字符串
     * @return true 全为数字 false 非全为数字
     * @description 判断字符串是否全为数字
     */
    public static boolean isNumeric(String str){
    
    
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
 /**
    * @param date 时间
    * @param year 年数
    * @return 增加之后的时间
    * @description 在date时间上增加多少年
    */
    public static Date dateAddYear(Date date ,Integer year){
    
    
        Calendar cal = Calendar.getInstance();
        //设置起始时间
        cal.setTime(date);
        //增加多少年
        cal.add(Calendar.YEAR, year);
        return cal.getTime();
    }
/**
     * @param str
     * @return true:是,false不是
     * @description 判断是否是整数或者是小数
     */
    public static boolean validateNumber(String str) {
    
    
        if(StringUtils.isBlank(str)) {
    
    
            return false;
        }
        // 说明一下的是该正则只能识别5位小数;如果不限制小数位数的话,写成[+-]?[0-9]+(\\.[0-9]+)?就可以了
        return str.matches("[+-]?[0-9]+(\\.[0-9]{1,6})?");
        
    }
/**
     * @param dateTime 时间
     * @return 字符串格式化时间
     * @description 将date时间转化成 yyyy-MM-dd HH:mm:ss 格式的字符串时间
     */
    public static String dateConversionStr(Date dateTime) {
    
    
        if (ObjectUtils.isEmpty(dateTime)){
    
    
            return "";
        }
        //定义转化为字符串的日期格式 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将时间转化为字符串格式日期
        return sdf.format(dateTime);
    }

猜你喜欢

转载自blog.csdn.net/weixin_56287335/article/details/126763081