package com.jiewen.mispos.api;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class PubTools {
private final static char[] mChars = "0123456789ABCDEF".toCharArray();
public static final byte[] iniKey = { 00, 00, 00, 00, 00, 00, 00, 00 };
public static final byte[] key = { 01, 03, 20, 34, 12, 40, 23, 50, 23, 22,
11, 26, 38, 49, 53, 33 };
/**
* 姣旇緝涓や釜鏃ユ湡澶у皬
*
* @param String
* @return
*/
public static int compareDateString(String st1Date1, String strDate2) {
return compareDateString(st1Date1, strDate2, "yyyy-MM-dd");
}
/**
* 姣旇緝涓や釜鏃ユ湡澶у皬
*/
public static int compareDateString(String st1Date1, String strDate2,
String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
int ret = 0;
try {
Date d1 = sdf.parse(st1Date1);
Date d2 = sdf.parse(strDate2);
long result = d1.getTime() - d2.getTime();
if (result > 0) {
ret = 1;
} else if (result < 0) {
ret = -1;
} else {
ret = 0;
}
} catch (ParseException e) {
e.printStackTrace();
}
return ret;
}
/**
* 鑾峰彇涓ゅぉ鐩稿樊鍑犲ぉ 鏍煎紡 yyyy-MM-dd HH:mm:ss
*
* @param startTime
* @param endTime
* @param format
*/
public static long dateDiff(String startTime, String endTime, String format) {
// 鎸夌収浼犲叆鐨勬牸寮忕敓鎴愪竴涓猻impledateformate瀵硅薄
long day = -9999;
try {
SimpleDateFormat df = new SimpleDateFormat(format);
java.util.Date startDate = df.parse(startTime);
java.util.Date endDate = df.parse(endTime);
day = getDifferDays(startDate, endDate);
} catch (Exception e) {
e.printStackTrace();
return -9999L;
}
return day;
}
public static long getDifferDays(Date startDate, Date endDate) {
long day = -9999L;
long l = startDate.getTime() - endDate.getTime();
day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
if (hour + min + s > 0) {
day = day + 1;
}
return day;
}
/**
* 鑾峰彇涓ゅぉ鐩稿樊鍑犲ぉ 鏍煎紡 yyyy-MM-dd HH:mm:ss
*
* @param startTime
* @param endTime
* @param format
*/
public static long dateDiff(String startTime, String endTime) {
// 鎸夌収浼犲叆鐨勬牸寮忕敓鎴愪竴涓猻impledateformate瀵硅薄
long day = -9999;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date startDate = df.parse(startTime);
java.util.Date endDate = df.parse(endTime);
day = getDifferDays(startDate, endDate);
} catch (Exception e) {
e.printStackTrace();
return -9999L;
}
return day;
}
/**
* 鍙栦笅鍑犲ぉ
*
* @param srtDate
* @param format
* @param i
* @return
*/
public static String getNextDay(String srtDate, String format, int i) {
Calendar cal = Calendar.getInstance();
Date date = new Date();
date = stringToDate(srtDate, format);
cal.setTime(date);
cal.add(cal.DATE, i);
return dateToString(cal.getTime(), format);
}
public static Date getNextDay(Date startDate, int i)
{
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.add(cal.DATE, i);
return cal.getTime();
}
/**
* 鍙栦笅涓?澶?
*
* @param srtDate
* @param format
* @param i
* @return
*/
public static String getNextDay(String srtDate, String format) {
return getNextDay(srtDate, format, 1);
}
/**
* 鍔熻兘锛氬湪缁欏畾鐨勭簿搴﹁寖鍥村唴姣旇緝涓や釜鍙岀簿搴︽暟鐨勫ぇ灏? 鍙傛暟锛? d1,d2:寰呮瘮杈冪殑鏁? scale:姣旇緝鍒板皬鏁扮偣鍚庝綅鏁? if d1>d2 return 1
* if d1=d2 return 0 if d1<d2 return -1 姣旇緝鏃跺灏忔暟浣嶆暟鐨勫悗涓?浣嶆寜鍥涜垗浜斿叆鍚庢瘮杈? 渚嬪: d1=2.115,
* d2=2.112, scale=2, 鍒? 杩斿洖涓?1锛岃〃绀篸1>d2, d1=2.114, d2=2.112, scale=2, 鍒?
* 杩斿洖涓?0,琛ㄧずd1=d2, d1=2.1149, d2=2.112, scale=2, 鍒? 杩斿洖涓?0,琛ㄧずd1=d2,
*
* @param d1
* @param d2
* @param scale
* @return
*/
public static int compareDouble(double d1, double d2, int scale) {
long l1, l2;
long times = Math.round(Math.pow(10, scale));
l1 = Math.round(d1 * times);
l2 = Math.round(d2 * times);
if (l1 > l2) {
return 1;
} else if (l1 == l2) {
return 0;
} else {
return -1;
}
}
/**
* 灏嗘棩鏈熻浆鎹㈡垚format鏍煎紡鐨勫瓧绗︿覆 yyyy-MM-dd HH:mm:ss
*
* @param date
* @param format
* @return
*/
public static String dateToString(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 灏嗗綋鍓嶆棩鏈熻浆鎹㈡垚浠绘剰鏍煎紡鐨勫瓧绗︿覆 yyyy-MM-dd HH:mm:ss
*
* @param format
* @return
*/
public static String dateToString(String format) {
return dateToString(new Date(), format);
}
/**
* 灏嗗綋鍓嶆棩鏈熻浆鎹㈡垚"yyyy-MM-dd"鏍煎紡鐨勫瓧绗︿覆
*
* @return
*/
public static String dateToString() {
return dateToString(new Date(), "yyyy-MM-dd");
}
/**
* 灏嗗綋鍓嶆棩鏈熻浆鎹㈡垚"yyyy-MM-dd"鏍煎紡鐨勬棩鏈?
*
* @return
*/
public static Date dateToDate() {
return stringToDate(dateToString(), "yyyy-MM-dd");
}
/**
* 灏嗗綋鍓嶆棩鏈熻浆鎹㈡垚浠讳竴鏍煎紡鐨勬棩鏈?
*
* @param format
* @return
*/
public static Date dateToDate(String format) {
return stringToDate(dateToString(format), format);
}
/**
* 瀛楃涓茶浆鎹㈡垚鏃ユ湡
*
* @param str
* @param toFormat
* @return
*/
public static Date stringToDate(String str, String toFormat) {
SimpleDateFormat format = new SimpleDateFormat(toFormat);
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
// *********************************************
/**
* 鏁村瀷鍓嶈ˉ0
*
* @param intFrom
* @param len
* @return
*/
public static String formatInt(int intFrom, int len) {
// 0 浠h〃鍓嶉潰琛ュ厖0
String strlen = "%0" + len + "d";
String strRet = String.format(strlen, intFrom);
return strRet;
}
/**
* 瀛楃涓插墠琛ョ┖鏍?
*
* @param str
* @param len
* @return
*/
public static String formatString(String str, int len) {
// 鍓嶉潰琛ュ厖绌烘牸
String strlen = "%" + len + "s";
String strRet = String.format(strlen, str);
return strRet;
}
/**
* 瀛楃涓插悗琛ョ┖鏍?
* @param str
* @param len
* @return
*/
public static String rightFormatString(String str, int len) {
// 鍓嶉潰琛ュ厖绌烘牸
String strlen = "%-" + len + "s";
String strRet = String.format(strlen, str);
return strRet;
}
/**
* 鏁村瀷鍓嶈ˉ0
*
* @param intFrom
* @return
*/
public static String formatInt(int intFrom) {
// 0 浠h〃鍓嶉潰琛ュ厖0
String strlen = "%03d";
String strRet = String.format(strlen, intFrom);
return strRet;
}
/**
* 16杩涘埗杞琲nt
*
* @param content
* @return
*/
public static int hexToInt(String content) {
if ("-1".equals(content.trim())) {
return -1;
} else {
return Integer.valueOf(content, 16);
}
}
/**
* int杞?16杩涘埗
*
* @param content
* @return
*/
public static String intToHex(int content) {
if (content == -1) {
return -1 + " ";
} else {
return flushLeft(Integer.toHexString(content).toUpperCase());
}
}
public static String intToHex(int content ,int len)
{
if (content == -1) {
return -1 + " ";
} else {
return flushLeft(Integer.toHexString(content).toUpperCase(),len);
}
}
/**
* String 宸﹁ˉ0
*
* @param content
* @return
*/
public static String flushLeft(String content) {
StringBuffer cs = new