java 帮助类

Wesley13
• 阅读 549
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;i < length;i ++){
         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;i < length;i ++){
         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;i < 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"));
     
 }
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这