Java开发常用工具类

1.MD5加密算法工具类

import org.springframework.util.StringUtils;
import java.security.MessageDigest;

public class MD5Util {
    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    /**
     * 返回大写MD5
     *
     * @param origin
     * @param charsetname
     * @return
     */
    private static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString.toUpperCase();
    }

    public static String MD5EncodeUtf8(String origin) {
    	//加盐的代码   adfjadkfqperuparjp   123456+mary  666  888 9999 iloveyou
        //origin = origin + PropertiesUtil.getProperty("password.salt", "");
        return MD5Encode(origin, "utf-8");
    }


    private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

}

2.时间转换工具类

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


public class DateTimeUtil {
	
	//定义默认日期格式
	private static String DATEFORMATUrl="yyyy-MM-dd HH:mm:ss";
	
	//Str-->Date
	public static Date strToDate(String strDate) {
		
		DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATEFORMATUrl);
		DateTime dateTime = dateTimeFormatter.parseDateTime(strDate);
		return dateTime.toDate();

	}
	
	//Str-->Date(自定义格式)
	public static Date strToDate(String strDate,String formatStr) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
		DateTime dateTime = dateTimeFormatter.parseDateTime(strDate);
		return dateTime.toDate();
	}
	
	//Date-->Str
	public static String dateToStr(Date date) {
		if(date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(DATEFORMATUrl);
	}
	
	//Date-->Str(自定义格式)	
	public static String dateToStr(Date date,String formatStr) {
		if(date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(formatStr);
	}
	
	public static void main(String[] args) {
		System.out.println(strToDate("2010-10-20 12:12:45", "yyyy-MM-dd HH:mm:ss"));//Wed Oct 20 12:12:45 CST 2010
		System.out.println(dateToStr(new Date(), "yyyy-MM-dd"));
		System.out.println(strToDate("2023-01-23 13:23:56"));
		System.out.println(dateToStr(new Date()));
	}	
}

3.解决价格精度丢失工具类(常用于商城)

import java.math.BigDecimal;

public class BigDecimalUtil {
	//加法
	public static BigDecimal add(double p1,double p2) {
		BigDecimal d1 = new BigDecimal(Double.toString(p1));
		BigDecimal d2 = new BigDecimal(Double.toString(p2));		
		return d1.add(d2);
	}
	
	//减法
	public static BigDecimal sub(double p1,double p2) {
		BigDecimal d1 = new BigDecimal(Double.toString(p1));
		BigDecimal d2 = new BigDecimal(Double.toString(p2));
		return d1.subtract(d2);
	}
	//乘法
	public static BigDecimal mul(double p1,double p2) {
		BigDecimal d1 = new BigDecimal(Double.toString(p1));
		BigDecimal d2 = new BigDecimal(Double.toString(p2));
		return d1.multiply(d2);

	}
	//除法
	public static BigDecimal div(double p1,double p2) {
		BigDecimal d1 = new BigDecimal(Double.toString(p1));
		BigDecimal d2 = new BigDecimal(Double.toString(p2));
		//BigDecimal.ROUND_HALF_UP四舍五入
		return d1.divide(d2, 2, BigDecimal.ROUND_HALF_UP);
	}
	
	public static void main(String[] args) {
		double d1 = 0.01;
		double d2 = 0.06;
		System.out.println(BigDecimalUtil.add(d1, d2));
	}
}

4.待更新…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值