public class TenpayUtil {
//private static Object Server;
/**
* 把对象转换成字符串
*
* @param obj
* @return String 转换成字符串,若对象为null,则返回空字符串.
*/
/*
public static String toString(Object obj) {
if (obj == null)
return "";
return obj.toString();
}*/
/**
* 把对象转换为int数值.
*
* @param obj
* 包含数字的对象.
* @return int 转换后的数值,对不能转换的对象返回0。
*/
/*
public static int toInt(Object obj) {
int a = 0;
try {
if (obj != null)
a = Integer.parseInt(obj.toString());
} catch (Exception e) {
}
return a;
}*/
/**
* 获取当前时间 yyyyMMddHHmmss
*
* @return String
*/
public static String getCurrTime() {
Date now = new Date();
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String s = outFormat.format(now);
return s;
}
/**
* 获取当前日期 yyyyMMdd
*
* @param date
* @return String
*/
public static String formatDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String strDate = formatter.format(date);
return strDate;
}
/**
* 取出一个指定长度大小的随机正整数.
*
* @param length
* int 设定所取出随机数的长度。length小于11
* @return int 返回生成的随机数。
*/
public static int buildRandom(int length) {
int num = 1;
double random = Math.random();
if (random < 0.1) {
random = random + 0.1;
}
for (int i = 0; i < length; i++) {
num = num * 10;
}
return (int) ((random * num));
}
/**
* 获取编码字符集
*
* @param request
* @param response
* @return String
*/
/*
public static String getCharacterEncoding(HttpServletRequest request,
HttpServletResponse response) {
if (null == request || null == response) {
return "gbk";
}
String enc = request.getCharacterEncoding();
if (null == enc || "".equals(enc)) {
enc = response.getCharacterEncoding();
}
if (null == enc || "".equals(enc)) {
enc = "gbk";
}
return enc;
}
public static String URLencode(String content) {
String URLencode;
URLencode = replace(Server.equals(content), "+", "%20");
return URLencode;
}*/
/*
private static String replace(boolean equals, String string, String string2) {
return null;
}
/
/*
* 获取unix时间,从1970-01-01 00:00:00开始的秒数
*
* @param date
* @return long
*/
public static long getUnixTime(Date date) {
if (null == date) {
return 0;
}
return date.getTime() / 1000;
}
/**
* 时间转换成字符串
*
* @param date
* 时间
* @param formatType
* 格式化类型
* @return String
*/
/*
public static String date2String(Date date, String formatType) {
SimpleDateFormat sdf = new SimpleDateFormat(formatType);
return sdf.format(date);
}*/
/**
* 获取随机字符串
*
* @return
*/
public static String getRandomStr() {
// 随机数
String currTime = TenpayUtil.getCurrTime();
// 8位日期
String strTime = currTime.substring(8, currTime.length());
// 四位随机数
String strRandom = TenpayUtil.buildRandom(4) + "";
// 10位序列号,可以自行调整。
return strTime + strRandom;
}
/**
* 元转换成分
*
* @param money
* @return
*/
public static String getMoney(String amount) {
if (amount == null) {
return "";
}
// 金额转化为分为单位
String currency = amount.replaceAll("\\$|\\¥|\\,", ""); // 处理包含, ¥
// 或者$的金额
int index = currency.indexOf(".");
int length = currency.length();
Long amLong = 0l;
if (index == -1) {
amLong = Long.valueOf(currency + "00");
} else if (length - index >= 3) {
amLong = Long.valueOf((currency.substring(0, index + 3)).replace(
".", ""));
} else if (length - index == 2) {
amLong = Long.valueOf((currency.substring(0, index + 2)).replace(
".", "") + 0);
} else {
amLong = Long.valueOf((currency.substring(0, index + 1)).replace(
".", "") + "00");
}
return amLong.toString();
}
//public static void main(String[] args) {
// System.out.println(getMoney("1"));
//}
/**
* description: 解析微信通知xml
*
* @param xml
* @return
* @see
*/
//@SuppressWarnings({"rawtypes", "unchecked" })
/*
public static Map parseXmlToList2(String xml) {
Map retMap = new HashMap();
try {
StringReader read = new StringReader(xml);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
// 创建一个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
// 通过输入源构造一个Document
Document doc = (Document) sb.build(source);
Element root = doc.getRootElement();// 指向根节点
List<Element> es = root.getChildren();
if (es != null && es.size() != 0) {
for (Element element : es) {
retMap.put(element.getName(), element.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return retMap;
}
*/
/*
public static String getTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}*/
/**
* 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
*
* @param strxml
* @return
* @throws JDOMException
* @throws IOException
*/
/*
public static Map<String,String> doXMLParse(String strxml) throws Exception {
if (null == strxml || "".equals(strxml)) {
return null;
}
Map<String,String> m = new HashMap<String,String>();
InputStream in = String2Inputstream(strxml);
Reader read = new InputStreamReader(in,"gbk");
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(read);
Element root = doc.getRootElement();
List<Element> list = root.getChildren();
Iterator<Element> it = list.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
String k = e.getName();
String v = "";
List<Element> children = e.getChildren();
if (children.isEmpty()) {
v = e.getTextNormalize();
} else {
v = getChildrenText(children);
}
m.put(k, v);
}
// 关闭流
in.close();
return m;
}
*/
/**
* 获取子结点的xml
*
* @param children
* @return String
*/
/*
public static String getChildrenText(List<Element> children) {
StringBuffer sb = new StringBuffer();
if (!children.isEmpty()) {
Iterator<Element> it = children.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
String name = e.getName();
String value = e.getTextNormalize();
List<Element> list = e.getChildren();
sb.append("<" + name + ">");
if (!list.isEmpty()) {
sb.append(getChildrenText(list));
}
sb.append(value);
sb.append("</" + name + ">");
}
}
return sb.toString();
}
*/
/*
public static InputStream String2Inputstream(String str) {
return new ByteArrayInputStream(str.getBytes());
}*/
}