java 帮助类

package com.quincy.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Field; import java.math.BigDecimal; import java.net.URL; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map.Entry; import java.util.Properties; /**  *   * @author Quincy  *  */ public class UtilHandler {    public static String QQ ;  public static String URLMAIL ;  public static String mailUserName ;  public static String mailUserPwd ;    //生成字符串  //检测字符串是否为空  //检测对象是否为空  //生成随机数  //格式化数据  //格式化日期  //BigDecimel  //日期转换为String类型  //yyyy-MM-dd HH:mm:ss SSS年月日   时分秒 毫秒  /**   * 为空返回false   * 不为空返回true   * @param strs   * @return   */  public static boolean checkString(String...strs){      if(strs != null && strs.length > 0){         for(String str : strs){            if(str == null || str.trim().equals("")){                return false;            }         }      }      return true;  }  /**   * 把传入的日期格式化为字符串   * @param date   * @param flag 有毫秒数   * @return   */  public static String getStringDate(Date date,boolean flag){       if(flag){        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS");        return sdf.format(date);       }else{        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");        return sdf.format(date);       }   }  /**   * 生成yyyy-MM-dd HH:mm:ss格式的日期   * @param date   * @return   */  public static String getStringDate(Date date){       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");       return sdf.format(date);  }  /**   *    * @param date   * @return   */  public static String getString(Date date){       SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");       return sdf.format(date);  }    /**   * 生成随机数   * 默认从0开始   * @param length 随机数的长度   * @param end 结束的数字   * @return   */  public static StringBuffer getRandom(int length,int end){       StringBuffer sb = new StringBuffer();       for (int i = 0; i < length; i++) {           int num = (int)(Math.random()*end);           sb.append(num);        }       return sb;  }    /**   *    * @param start 起始数   * @param length 产生随机数字的长度   * @param end  结束数   * @return   */  public static StringBuffer getRandom(int start,int length,int end){       StringBuffer sb = new StringBuffer();       for (int i = 0; i < length; i++) {           int num = (int)(Math.random()*end) + start;           sb.append(num);        }       return sb;  }    /**   * 数字格式化   * @param args   */  public static Object formatNumber(Object num){       DecimalFormat df = new DecimalFormat("###,###");       return df.format(num);  }    /**   * 数字格式化,保留小数点后边的几位   * @param args   */  public static Object formatNumberHaveDecimal(Object num,int length){       StringBuffer str = new StringBuffer();       for(int i = 0;< length;++){          str.append("#");       }       DecimalFormat df = new DecimalFormat("###,###." + str);       return df.format(num);  }  /**   * 位数不够补0   * @param num   * @param length   * @return   */  public static Object formatNumberByZero(Object num,int length){       StringBuffer str = new StringBuffer();       for(int i = 0;< length;++){          str.append("0");       }       DecimalFormat df = new DecimalFormat("###,###." + str);       return df.format(num);  }    /**   * 两个大数子相加   * @param str1   * @param str2   * @return   */  public static BigDecimal addBigDecimal(String str1,String str2){       BigDecimal big = new BigDecimal(str1);       BigDecimal smail = new BigDecimal(str2);       BigDecimal result = big.add(smail);       return result;  }    /**   * 两个大数字相减   * @param str1   * @param str2   * @return   */  public static BigDecimal subtractBigDecimal(String str1,String str2){       BigDecimal big = new BigDecimal(str1);       BigDecimal smail = new BigDecimal(str2);       BigDecimal result = big.subtract(smail);       return result;  }    /**   * 两个大数字乘   * @param str1   * @param str2   * @return   */  public static BigDecimal multiplyBigDecimal(String str1,String str2){       BigDecimal big = new BigDecimal(str1);       BigDecimal smail = new BigDecimal(str2);       BigDecimal result = big.multiply(smail);       return result;  }    /**   * 两个大数字相除   * @param str1   * @param str2   * @return   */  public static BigDecimal divideBigDecimal(String str1,String str2){       BigDecimal big = new BigDecimal(str1);       BigDecimal smail = new BigDecimal(str2);       //除法报错(无法整除),确定要保留的小数位       BigDecimal result = big.divide(smail,4,BigDecimal.ROUND_HALF_UP);                return result;  }    /**   * 两个大数字相除   * @param str1   * @param str2   * @param length 设置保留的小数点之后的位数   * @return   */  public static BigDecimal divideBigDecimal(String str1,String str2,int length){       BigDecimal big = new BigDecimal(str1);       BigDecimal smail = new BigDecimal(str2);       //除法报错(无法整除),确定要保留的小数位       BigDecimal result = big.divide(smail,length);       return result;  }    public static String myNumber(int length,int number){       String strs = getString(new Date()) + getRandom(length,number).toString();       return strs;  }   /**   *    * 如果要处理多个properties文件   * @param propertiesName   */  public static void readProperties(String...propertiesName) {       System.out.println("加载相关资源文件开始!");       Properties properties = new Properties();       FileInputStream in = null;       try {            //循环是为了处理对个properties文件            for(int i = 0;< propertiesName.length ; i ++){               URL urlPath = UtilHandler.class.getClassLoader().getResource(propertiesName[i]);               properties.load(new FileInputStream(new File(urlPath.getFile())));               getValue(properties);            }       } catch (FileNotFoundException e) {            e.printStackTrace();       } catch (IOException e) {            e.printStackTrace();       } finally {            try {               if (in != null) {                  in.close();               }            } catch (IOException e) {               e.printStackTrace();            }        }  }    /**   * 获取Properties文件   * @param propertiesName   */  public static void readProperties(String propertiesName){       System.out.println("加载相关资源文件开始!");       Properties  properties = new Properties();       FileInputStream in = null;          try {             URL urlPath = UtilHandler.class.getClassLoader().getResource(propertiesName);             properties.load(new FileInputStream(new File(urlPath.getFile())));             getValue(properties);          } catch (FileNotFoundException e) {               e.printStackTrace();          } catch (IOException e) {               e.printStackTrace();          }finally{          try {             if(in != null){                in.close();             }          } catch (IOException e) {               e.printStackTrace();          }       } }    /**     * 由传入的Properties文件中的key获取对应的value     * @param key     * @return      */   public static void getValue(Properties properties){            for(Entry<Object, Object> key : properties.entrySet()){             try {                 String myKey = (String)key.getKey();                 Class<?> clazz = Class.forName("com.quincy.util.UtilHandler");                 Field[] fields = clazz.getFields();                 for(Field f: fields){                     try {                         if(myKey.toLowerCase().equals(f.getName().toLowerCase())){                             f.set(f.getName(), key.getValue());                         }                     } catch (Exception e) {                         e.printStackTrace();                     }                  }             } catch (ClassNotFoundException e) {              e.printStackTrace();             }        }  }   Calendar c = new GregorianCalendar();   Calendar cs = new GregorianCalendar(1990,11,2);   public static String formatDuring(long mss) {          long days = mss / (1000 * 60 * 60 * 24);          long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);          long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);          long seconds = (mss % (1000 * 60)) / 1000;          return " days:"+ days + " hours :" + hours + " minutes " + minutes +                  + " seconds "+ seconds;      }    /**   *    * @param args   */     public static void main(String[] args) {            /*String str = getString(new Date());      System.out.println(str);      System.out.println(formatNumberByZero(12456898798.14,5));      System.out.println(myBigDecimal("13.1","2.25"));      StringBuffer sb = getRandom(8,10);            System.out.println(Integer.valueOf(sb.toString()) + "============");      System.out.println(myNumber(5,10));*/            /**       * 2014 04 25 23 49 57 329 60152       * 2014 04 25 23 50 26 172 68544       * 2014 04 25 23 50 35 432 60071       * 2014 04 25 23 51 47 224 88742       * 2014 04 25 23 52 28 947 76036526       */            readProperties("resources.properties");      System.out.println(QQ);      System.out.println(URLMAIL);      System.out.println(mailUserName);      System.out.println(mailUserPwd + " +++++++++");            System.out.println(divideBigDecimal("1.23","5.69"));        } }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )