java 帮助类

1package com.quincy.util; 2import java.io.File; 3import java.io.FileInputStream; 4import java.io.FileNotFoundException; 5import java.io.IOException; 6import java.lang.reflect.Field; 7import java.math.BigDecimal; 8import java.net.URL; 9import java.text.DecimalFormat; 10import java.text.SimpleDateFormat; 11import java.util.Date; 12import java.util.Map.Entry; 13import java.util.Properties; 14/** 15 *  16 * @author Quincy 17 * 18 */ 19public class UtilHandler { 20  21 public static String QQ ; 22 public static String URLMAIL ; 23 public static String mailUserName ; 24 public static String mailUserPwd ; 25  26 //生成字符串 27 //检测字符串是否为空 28 //检测对象是否为空 29 //生成随机数 30 //格式化数据 31 //格式化日期 32 //BigDecimel 33 //日期转换为String类型 34 //yyyy-MM-dd HH:mm:ss SSS年月日   时分秒 毫秒 35 /** 36  * 为空返回false 37  * 不为空返回true 38  * @param strs 39  * @return 40  */ 41 public static boolean checkString(String...strs){ 42     if(strs != null && strs.length > 0){ 43        for(String str : strs){ 44           if(str == null || str.trim().equals("")){ 45               return false; 46           } 47        } 48     } 49     return true; 50 } 51 /** 52  * 把传入的日期格式化为字符串 53  * @param date 54  * @param flag 有毫秒数 55  * @return 56  */ 57 public static String getStringDate(Date date,boolean flag){ 58      if(flag){ 59       SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS"); 60       return sdf.format(date); 61      }else{ 62       SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 63       return sdf.format(date); 64      } 65  } 66 /** 67  * 生成yyyy-MM-dd HH:mm:ss格式的日期 68  * @param date 69  * @return 70  */ 71 public static String getStringDate(Date date){ 72      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 73      return sdf.format(date); 74 } 75 /** 76  *  77  * @param date 78  * @return 79  */ 80 public static String getString(Date date){ 81      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); 82      return sdf.format(date); 83 } 84  85 /** 86  * 生成随机数 87  * 默认从0开始 88  * @param length 随机数的长度 89  * @param end 结束的数字 90  * @return 91  */ 92 public static StringBuffer getRandom(int length,int end){ 93      StringBuffer sb = new StringBuffer(); 94      for (int i = 0; i < length; i++) { 95          int num = (int)(Math.random()*end); 96          sb.append(num);  97      } 98      return sb; 99 } 100  101 /** 102  *  103  * @param start 起始数 104  * @param length 产生随机数字的长度 105  * @param end  结束数 106  * @return 107  */ 108 public static StringBuffer getRandom(int start,int length,int end){ 109      StringBuffer sb = new StringBuffer(); 110      for (int i = 0; i < length; i++) { 111          int num = (int)(Math.random()*end) + start; 112          sb.append(num);  113      } 114      return sb; 115 } 116  117 /** 118  * 数字格式化 119  * @param args 120  */ 121 public static Object formatNumber(Object num){ 122      DecimalFormat df = new DecimalFormat("###,###"); 123      return df.format(num); 124 } 125  126 /** 127  * 数字格式化,保留小数点后边的几位 128  * @param args 129  */ 130 public static Object formatNumberHaveDecimal(Object num,int length){ 131      StringBuffer str = new StringBuffer(); 132      for(int i = 0;i < length;i ++){ 133         str.append("#"); 134      } 135      DecimalFormat df = new DecimalFormat("###,###." + str); 136      return df.format(num); 137 } 138 /** 139  * 位数不够补0 140  * @param num 141  * @param length 142  * @return 143  */ 144 public static Object formatNumberByZero(Object num,int length){ 145      StringBuffer str = new StringBuffer(); 146      for(int i = 0;i < length;i ++){ 147         str.append("0"); 148      } 149      DecimalFormat df = new DecimalFormat("###,###." + str); 150      return df.format(num); 151 } 152  153 /** 154  * 两个大数子相加 155  * @param str1 156  * @param str2 157  * @return 158  */ 159 public static BigDecimal addBigDecimal(String str1,String str2){ 160      BigDecimal big = new BigDecimal(str1); 161      BigDecimal smail = new BigDecimal(str2); 162      BigDecimal result = big.add(smail); 163      return result; 164 } 165  166 /** 167  * 两个大数字相减 168  * @param str1 169  * @param str2 170  * @return 171  */ 172 public static BigDecimal subtractBigDecimal(String str1,String str2){ 173      BigDecimal big = new BigDecimal(str1); 174      BigDecimal smail = new BigDecimal(str2); 175      BigDecimal result = big.subtract(smail); 176      return result; 177 } 178  179 /** 180  * 两个大数字乘 181  * @param str1 182  * @param str2 183  * @return 184  */ 185 public static BigDecimal multiplyBigDecimal(String str1,String str2){ 186      BigDecimal big = new BigDecimal(str1); 187      BigDecimal smail = new BigDecimal(str2); 188      BigDecimal result = big.multiply(smail); 189      return result; 190 } 191  192 /** 193  * 两个大数字相除 194  * @param str1 195  * @param str2 196  * @return 197  */ 198 public static BigDecimal divideBigDecimal(String str1,String str2){ 199      BigDecimal big = new BigDecimal(str1); 200      BigDecimal smail = new BigDecimal(str2); 201      //除法报错(无法整除),确定要保留的小数位 202      BigDecimal result = big.divide(smail,4,BigDecimal.ROUND_HALF_UP); 203         204      return result; 205 } 206  207 /** 208  * 两个大数字相除 209  * @param str1 210  * @param str2 211  * @param length 设置保留的小数点之后的位数 212  * @return 213  */ 214 public static BigDecimal divideBigDecimal(String str1,String str2,int length){ 215      BigDecimal big = new BigDecimal(str1); 216      BigDecimal smail = new BigDecimal(str2); 217      //除法报错(无法整除),确定要保留的小数位 218      BigDecimal result = big.divide(smail,length); 219      return result; 220 } 221  222 public static String myNumber(int length,int number){ 223      String strs = getString(new Date()) + getRandom(length,number).toString(); 224      return strs; 225 } 226  227/** 228  *  229  * 如果要处理多个properties文件 230  * @param propertiesName 231  */ 232 public static void readProperties(String...propertiesName) { 233      System.out.println("加载相关资源文件开始!"); 234      Properties properties = new Properties(); 235      FileInputStream in = null; 236      try { 237           //循环是为了处理对个properties文件 238           for(int i = 0;i < propertiesName.length ; i ++){ 239              URL urlPath = UtilHandler.class.getClassLoader().getResource(propertiesName[i]); 240              properties.load(new FileInputStream(new File(urlPath.getFile()))); 241              getValue(properties); 242           } 243      } catch (FileNotFoundException e) { 244           e.printStackTrace(); 245      } catch (IOException e) { 246           e.printStackTrace(); 247      } finally { 248           try { 249              if (in != null) { 250                 in.close(); 251              } 252           } catch (IOException e) { 253              e.printStackTrace(); 254           } 255       } 256 } 257  258 /** 259  * 获取Properties文件 260  * @param propertiesName 261  */ 262 public static void readProperties(String propertiesName){ 263      System.out.println("加载相关资源文件开始!"); 264      Properties  properties = new Properties(); 265      FileInputStream in = null; 266         try { 267            URL urlPath = UtilHandler.class.getClassLoader().getResource(propertiesName); 268            properties.load(new FileInputStream(new File(urlPath.getFile()))); 269            getValue(properties); 270         } catch (FileNotFoundException e) { 271              e.printStackTrace(); 272         } catch (IOException e) { 273              e.printStackTrace(); 274         }finally{ 275         try { 276            if(in != null){ 277               in.close(); 278            } 279         } catch (IOException e) { 280              e.printStackTrace(); 281         } 282      } 283 284} 285   /** 286    * 由传入的Properties文件中的key获取对应的value 287    * @param key 288    * @return  289    */ 290  public static void getValue(Properties properties){ 291    292       for(Entry<Object, Object> key : properties.entrySet()){ 293            try { 294                String myKey = (String)key.getKey(); 295                Class<?> clazz = Class.forName("com.quincy.util.UtilHandler"); 296                Field[] fields = clazz.getFields(); 297                for(Field f: fields){ 298                    try { 299                        if(myKey.toLowerCase().equals(f.getName().toLowerCase())){ 300                            f.set(f.getName(), key.getValue()); 301                        } 302                    } catch (Exception e) { 303                        e.printStackTrace(); 304                    } 305                 } 306            } catch (ClassNotFoundException e) { 307             e.printStackTrace(); 308            } 309       } 310 311 } 312  Calendar c = new GregorianCalendar(); 313  Calendar cs = new GregorianCalendar(1990,11,2); 314  public static String formatDuring(long mss) { 315         long days = mss / (1000 * 60 * 60 * 24); 316         long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); 317         long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60); 318         long seconds = (mss % (1000 * 60)) / 1000; 319         return " days:"+ days + " hours :" + hours + " minutes " + minutes +                  + " seconds "+ seconds; 320     } 321  322 /** 323  *  324  * @param args 325  */ 326    public static void main(String[] args) { 327      328     /*String str = getString(new Date()); 329     System.out.println(str); 330     System.out.println(formatNumberByZero(12456898798.14,5)); 331     System.out.println(myBigDecimal("13.1","2.25")); 332     StringBuffer sb = getRandom(8,10); 333      334     System.out.println(Integer.valueOf(sb.toString()) + "============"); 335     System.out.println(myNumber(5,10));*/ 336      337     /** 338      * 2014 04 25 23 49 57 329 60152 339      * 2014 04 25 23 50 26 172 68544 340      * 2014 04 25 23 50 35 432 60071 341      * 2014 04 25 23 51 47 224 88742 342      * 2014 04 25 23 52 28 947 76036526 343      */ 344      345     readProperties("resources.properties"); 346     System.out.println(QQ); 347     System.out.println(URLMAIL); 348     System.out.println(mailUserName); 349     System.out.println(mailUserPwd + " +++++++++"); 350      351     System.out.println(divideBigDecimal("1.23","5.69")); 352      353 } 354}
点赞
收藏

评论区

加载中...

相关推荐

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 )

java 帮助类 - HelloWorld