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];
}
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) {
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";
public static Date strToDate(String strDate) {
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATEFORMATUrl);
DateTime dateTime = dateTimeFormatter.parseDateTime(strDate);
return dateTime.toDate();
}
public static Date strToDate(String strDate,String formatStr) {
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
DateTime dateTime = dateTimeFormatter.parseDateTime(strDate);
return dateTime.toDate();
}
public static String dateToStr(Date date) {
if(date == null) {
return StringUtils.EMPTY;
}
DateTime dateTime = new DateTime(date);
return dateTime.toString(DATEFORMATUrl);
}
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"));
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));
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.待更新…