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"));
}
}
java 帮助类
点赞
收藏